gpt4 book ai didi

ruby-on-rails - 使用设计 Rails 多个注册路径

转载 作者:行者123 更新时间:2023-12-03 17:33:23 25 4
gpt4 key购买 nike

我在 rails 中使用 devise,我正在尝试创建第二个注册路径,用户的默认注册路径(完美运行),以及第二个注册路径,用户可以在单个操作中注册和创建项目作为“入门”行动(想想 Freelancer.com 或任何非用户可以在一个行动中注册和创建他们的第一个项目的市场)。

我经历了很多线程,但它们似乎都想替换现有的注册操作。我想保留现有的注册操作并添加 getting_started_newgetting_started_create对现有的操作 User::Registration Controller ,或创建一个新的 GettingStarted带有 new 方法的 Controller 和 create这将允许嵌套表单(对于用户和他们的新项目)。

我最近的迭代看起来像这样:

路线.rb

devise_for :users

resources :users do
resources :projects
end

devise_scope :user do
get "getting_started" =>'getting_started#new'
post "getting_started" =>'getting_started#create'
end

Getting_started_controller.rb
class GettingStartedController < Devise::RegistrationsController

def new
@user = User.new
@user.projects.build
end

def create
@user = User.new(getting_started_params)
redirect_to root, notice: "Done"
end

private
def getting_started_params
params.require(:user).permit(:first_name, :last_name, :phone, :password, :email, projects_attributes: [:user_id, :project_type_id, :name, :industry_id, :description, :budget_id, :project_status_id, feature_ids:[], addon_ids:[]])
end

end

但是当我尝试实际提交表单时,它加载了 get来自新 Controller 的操作,以及 post来自设备注册 Controller 的操作。
Started GET "/getting_started" for ::1 at 2016-10-17 09:15:58 +0200
Processing by GettingStartedController#new as HTML
Rendering getting_started/new.html.erb within layouts/application
Project_type Load (0.5ms) SELECT "project_types".* FROM "project_types"
Industry Load (1.1ms) SELECT "industries".* FROM "industries" ORDER BY "industries"."name" ASC
Feature Load (0.5ms) SELECT "features".* FROM "features" ORDER BY "features"."name" ASC
Addon Load (0.4ms) SELECT "addons".* FROM "addons"
Budget Load (0.4ms) SELECT "budgets".* FROM "budgets"
Rendered getting_started/new.html.erb within layouts/application (32.9ms)
Rendered layouts/_header.html.erb (1.9ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 97ms (Views: 86.4ms | ActiveRecord: 2.9ms)


Started POST "/users" for ::1 at 2016-10-17 09:16:54 +0200
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hYrC/15zIF3DzPvhpFQH6LVS9H8XbhfHY1aRkgc2iX6Mdfvz33/O4p72XTpmY4X8E6B+dGfOmjZw7IoizvYwSA==", "user"=>{"first_name"=>"John", "last_name"=>"Smith", "phone"=>"0340239402309", "email"=>"test@example.com", "password"=>"[FILTERED]", "project"=>{"name"=>"This is a test", "description"=>"Tester Description", "feature_ids"=>[""], "addon_ids"=>[""]}}, "project"=>{"project_type_id"=>"4", "industry_id"=>"2", "budget_id"=>"1"}, "commit"=>"Create account"}
Unpermitted parameter: project
(0.1ms) BEGIN
SQL (1.5ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "first_name", "last_name", "phone") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["email", "test@example.com"], ["encrypted_password", "$2a$11$VEVjSrnc5Y5c8ofXvNmlMOHrS.v4tQuBoKMLdHaSOA2S6fiVIHfE."], ["created_at", 2016-10-17 07:16:55 UTC], ["updated_at", 2016-10-17 07:16:55 UTC], ["first_name", "John"], ["last_name", "Smith"], ["phone", "0340239402309"]]
(139.1ms) COMMIT
(0.1ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "users"."id" = $7 [["sign_in_count", 1], ["current_sign_in_at", 2016-10-17 07:16:55 UTC], ["last_sign_in_at", 2016-10-17 07:16:55 UTC], ["current_sign_in_ip", "::1/128"], ["last_sign_in_ip", "::1/128"], ["updated_at", 2016-10-17 07:16:55 UTC], ["id", 17]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 319ms (ActiveRecord: 141.7ms)

我意识到除了简单地记录用户数据之外,devise 还做了很多其他的事情,所以最好将这些操作移到 Devise User::RegistrationController ,然后调整接受的参数。但是你如何覆盖设计,以便它不会自动使用设计 create在您单击提交按钮的第二个方法中,将改为使用 getting_started_create行动?

最佳答案

首先getting started对于 Controller 来说是一个非常可怕的名字,因为它不是名词。你不能说是入门。因此,让我们将其更改为 QuickstartsController。

让我们为这个资源设置路由:

Rails.application.routes.draw do

# These are your "normal" devise routes
devise_for :users

# This is the mapping for the "quickstart" routes
devise_for :quickstarts,
class_name: 'User',
only: [],
controllers: { registrations: 'quickstarts' }

# These are the actual routes for quickstarts
devise_scope :quickstart do
get "/quickstarts/new", to: "quickstarts#new", as: :new_quickstart
post "/quickstarts", to: "quickstarts#create", as: :quickstarts
end
end

每当您覆盖库 Controller 时,花一些时间研究如何 the class you are superclassing 很重要。实际上有效。

如果你看一下设计 Controller ,几乎所有的 Action 都有这样的一行:
yield resource if block_given?

这让您可以通过调用 super 来了解原始实现的流程。带块:
class QuickstartsController < Devise::RegistrationsController

# GET /quickstarts/new
def new
# This block is passed to the super class implementation:
super do |resource|
resource.projects.build
end
end

# POST /quickstarts
def create
# You can pass a block here too if you want.
super
end

private

# This should redirect to the newly created project
def after_sign_up_path_for(resource)
polymorphic_path( resource.projects.last )
end

# This is the method Devise devise calls for the params.
def sign_up_params
# Don't write your params sanitiation on one line! Its not cool.
params.require(:user)
.permit(
:first_name, :last_name, :phone, :password, :email,
projects_attributes: [
:user_id, :project_type_id, :name,
:industry_id, :description, :budget_id,
:project_status_id,
feature_ids:[],
addon_ids:[]
]
)
end
end

But how do you override Devise so that it doesn't auto use the devise create method the second you click the submit button, and would instead use a getting_started_create action?



将表单指向正确的 Controller - 通过传递 url选项 form_for .

应用程序/ View /quickstarts/new.html.erb:
<h2>Getting Started</h2>

<%= form_for( resource, as: :user, url: quickstarts_path ) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<fieldset>
<legend>Project</legend>
<% f.fields_for :projects do |pf| %>
<%# ... %>
<% end %>
</fieldset>

<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>

关于ruby-on-rails - 使用设计 Rails 多个注册路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40080771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com