Reputation: 19294
I have a url like this http://example.com/myController?key=12345
In my controller myController
, i can access the key value with params[:key]
, but when i submit a form, i want to get that same key in the create
method, but the params[:key]
is null.
How can i access params[:key]
in my post action create
?
Upvotes: 1
Views: 1004
Reputation: 25757
You might indeed add a hidden field.
If you find yourself adding many of those because you need to have the value available in more and more actions, consider setting it within the session as (e.g.) session[:current_key]
Upvotes: 2
Reputation: 2385
Add a new hidden field to pass via params, when you submit the form your action is changed or called again params hash is recreated; what you can do is pass the value via hidden field in the form as follow,
<%= simple_form_for @model do |f| %>
# your remaining form fields
<%= f.input :field_name, as: :hidden, :input_html: { value: params[:key] } %>
<%= f.button :submit %>
<% end %>
Upvotes: 1
Reputation: 878
You can pass the info along by a hidden_field_tag
. So your code would be like:
<%= form_for @model do |f| %>
<%= hidden_field_tag 'key', params[:key]
# other form code
<%= f.submit %>
<% end %>
Upvotes: 1
Reputation: 29291
You can add a hidden field to your form and set its value to params[:key]
. Then it will be available in the params you get from the form.
If you're using Simple Form:
<%= simple_form_for @model do |f| %>
...
<%= f.input :field_name, :as => :hidden, :input_html => { :value => params[:key] } %>
<%= f.button :submit %>
<% end %>
Upvotes: 1