John
John

Reputation: 13725

How to use hidden_field to pass a parameter from new action to create action

I am calling the new action with the following:

= link_to 'Add Post', new_post_path(:user => { :user_id => params[:id] })

Such that the query string is:

?user%5Buser_id%5D=1

And putting the following in the form partial:

  .field
    = f.hidden_field :user_id

But after submitting the form I get an error because the user_id is not being set in the params. What am I doing wrong?

Upvotes: 1

Views: 1372

Answers (1)

David Sulc
David Sulc

Reputation: 25994

You have to set the hidden field's value. See http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag

In your controller:

@user_id = params[:user_id]

In your view:

.field
  = f.hidden_field :user_id, @user_id

Upvotes: 2

Related Questions