2

我正在使用带有 wicked 的设计来创建注册向导,但我不确定我在创建配置文件时遇到的问题。在用户提供他们的电子邮件和密码后,他们将被转发到一个步骤,以根据他们是否指定他们是托运人或承运人来创建配置文件。但是我不确定控制器中的代码应该是什么以及一般创建配置文件的表单。这是我的应用程序代码:

步数控制器:

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 控制器没有 POST 方法,尽管如果我运行 rake 路由,我发现这是不正确的。

4

1 回答 1

3

添加一个步骤 :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

意见/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 %>    

意见/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 %>

意见/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 %>
于 2012-06-01T09:42:50.240 回答