2016-12-01 40 views
0

我有註冊用戶窗體,當這個網址有得到的參數一樣 http://localhost:3000/signup?list_id=10 需要這個參數傳輸到行動在這次行動用戶創建用戶Rails的提交形式獲取參數的URL

<%= form_for(@user) do |f| %> 
<%= render 'shared/error_messages', user: @user %> 

<%= f.label :name %> 
<%= f.text_field :name, class: 'form-control' %> 

<%= f.label :email %> 
<%= f.email_field :email, class: 'form-control' %> 

<%= f.label :password %> 
<%= f.password_field :password, class: 'form-control' %> 

<%= f.label :password_confirmation %> 
<%= f.password_field :password_confirmation, class: 'form-control' %> 

<%= f.submit yield(:button_text), class: "btn btn-primary" %> 

創建沒有看到這個參數

def create 
debugger 
@user = User.new(user_params) 
if @user.save 
    log_in @user 
    flash[:success] = "Welcome to the Sample App!" 
    redirect_to @user 
else 
    render "new" 
end 

(byebug) params<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"I5+lc0T2fqy2Ie56rMnkR6Eff60nJmiuaTob7xAqknB6YgHZpmEByyRpCanpnNqyO9H/wMWbm7IumdXRyUABcA==", "user"=>{"name"=>"Ivan", "email"=>"[email protected]", "password"=>"222", "password_confirmation"=>"222"}, "commit"=>"Create my account", "controller"=>"users", "action"=>"create"} permitted: false> 

回答

1

它在你的params散列中可用,所以你可以將它作爲一個隱藏字段添加到你的表單中,因此它將與其他params一起提交。然後確保在控制器中將該參數列入白名單。

首先,在您的用戶模型中添加虛擬屬性,如果:list_id尚不是用戶的持久屬性。這是通過添加下面一行在user.rb模型文件進行:

attr_accessor :list_id 

然後添加以下表單視圖中:

<%= f.hidden_field :list_id, :value => params[:list_id] %> 

然後在你的控制器,你白名單的PARAMS你會將list_id添加爲安全參數。你沒有發佈你的strong_params方法,但應該是這樣的:

def user_params 
    params.require(:user).permit(:list_id, :name, :email, :password, :password_confirmation) 
end 
+1

它的工作,thnks –