从link_to传递params到注册表单(设计)

时间:2016-03-13 18:09:15

标签: ruby-on-rails authentication devise params

我们创建了包含名为type的附加字段的设计用户模型,用于区分不同类型的用户。我有register路线,您可以在其中选择要注册的人。该页面包含2个link_to,它会重定向到相同的设计用户表单。我想将类型参数传递给覆盖的寄存器控制器,以便自动分配类型。

class RegistrationsController < Devise::RegistrationsController

private

    def sign_up_params
        #assiging type
        params.require(:user).permit(:type, :email, :password, :password_confirmation)
    end
end

/注册

<%= link_to "Register as Developer", new_user_registration_path(:user => {:type => "developer")}) %>
<%= link_to "Register as Designer", new_user_registration_path(:user => {:type => "designer")}) %>

1 个答案:

答案 0 :(得分:0)

试试这个:

 <%= link_to "Register as Developer", new_user_registration_path({:user => {:type => "developer"}}) %>
 <%= link_to "Register as Designer", new_user_registration_path({:user => {:type => "designer"}}) %>

添加此代码:

  class ApplicationController < ActionController::Base
     before_action :configure_devise_permitted_parameters, if: :devise_controller?

  protected

   def configure_devise_permitted_parameters
     registration_params = [:type, :email, :password, :password_confirmation]

     if params[:action] == 'update'
       devise_parameter_sanitizer.for(:account_update) { 
        |u| u.permit(registration_params << :current_password)
       }
    elsif params[:action] == 'create'
       devise_parameter_sanitizer.for(:sign_up) { 
        |u| u.permit(registration_params) 
       }
   end
  end
 end