ruby-on-rails - 将 Wicked 与 Devise 结合使用作为注册向导

标签 ruby-on-rails ruby-on-rails-3 devise wizard

我正在使用 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/

相关文章:

ruby-on-rails - 使用 better_errors 作为调试器?

ruby-on-rails - rails 关系

ruby-on-rails - f.check_box checked => true for ruby​​ on rails

ruby-on-rails - 如何使用设备发送重置密码链接?

ruby-on-rails - 配置 nginx 以代理 thin 和 Rails ActionCable

mysql导入将时间戳空值转换为0000-00-00 00 :00:00

MySQL - 根据周/月数字获取 sum()

ruby-on-rails - 过滤器链停止为 :verify_signed_out_user rendered or redirected error when making a delete fetch in Rails Devise

ruby-on-rails - 设计 rails : Do not allow users to login whose role field is empty or nil

ruby-on-rails - ActiveRecord::InvalidForeignKey 错误