我正在使用 devise 和 wicked 来创建注册向导,但我不确定创建配置文件时遇到的问题。用户提供电子邮件和密码后,他们将转到一个步骤,根据他们是否指定自己是托运人或承运人来创建个人资料。但是我不确定 Controller 中应该包含什么代码以及通常创建配置文件的表单。这是我的应用程序代码:
步骤 Controller :
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :carrier_profile, :shipper_profile
def create
@user = User.last
case step
when :carrier_profile
@profile = CarrierProfile.create!(:dot => params[:dot])
if @profile.save
render_wizard @user
else
flash[:alert] = "Record not saved"
end
when :shipper_profile
@profile = ShipperProfile.create!(params[:shipper_profile)
if @profile.save
render_wizard @user
else
flash[:alert] = "Record not saved"
end
end
end
结束 结束
def show
@user = User.last
@carrier_profile = CarrierProfile.new
@shipper_profile = ShipperProfile.new
case step
when :carrier_profile
skip_step if @user.shipper?
when :shipper_profile
skip_step if @user.carrier?
end
render_wizard
end
end
运营商资料表格:
<% form_for @carrier_profile , url: wizard_path, method: :post do |f| %>
<div>
<%= f.label :dot, "Please enter your DOT Number:" %>
<%= f.text_field :dot %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
托运人资料表格:
<% form_for @shipper_profile , url: wizard_path, method: :post do |f| %>
<div>
<%= f.label :company_name, "What is your company name?" %>
<%= f.text_field :company_name %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
用户模型:
class User < ActiveRecord::Base
has_one :carrier_profile
has_one :shipper_profile
end
我如何能够编写一个通用的 new 和 create 方法来处理创建两个配置文件?使用当前代码,它表明 user_steps Controller 没有 POST 方法,尽管如果我运行 rake 路由,我发现这是不正确的。
最佳答案
添加一个步骤:profile_select 来查找应创建哪个配置文件
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :profile_select, :carrier_profile, :shipper_profile
def update
@user = current_user
case step
when :profile_select
if params[:user][:profile_type] == 'carrier'
@profile = current_user.carrier_profile.build
else
@profile = current_user.shipper_profile.build
end
when :carrier_profile
current_user.carrier_profile.update_attributes(params[:carrier_profile])
when :shipper_profile
current_user.shipper_profile.update_attributes(params[:shipper_profile])
end
render_wizard @user
end
def show
@user = current_user
case step
when :carrier_profile
skip_step if @user.shipper?
when :shipper_profile
skip_step if @user.carrier?
end
render_wizard
end
end
views/user_steps/profile_select.html.erb:
注意:方法 :put 以便为所有步骤调用更新
<%= form_for current_user, url: wizard_path, method: :put do |f| %>
<%= f.label :profile_type %>
<%= f.text_field :profile_type %>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
views/user_steps/carrier_profile.html.erb:
<% form_for @profile, url: wizard_path, method: :put do |f| %>
<div>
<%= f.label :dot, "Please enter your DOT Number:" %>
<%= f.text_field :dot %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
views/user_steps/shipper_profile.html.erb:
<% form_for @profile, url: wizard_path, method: :put do |f| %>
<div>
<%= f.label :company_name, "What is your company name?" %>
<%= f.text_field :company_name %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
关于ruby-on-rails - 将 Wicked 与 Devise 结合使用作为注册向导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843703/