Elliot Elliot - 6 months ago 14
Ruby Question

Send an extra parameter through a form in rails 3

Is there a way to send an extra parameter through a form in rails 3?

For example:

<%= form_for @post do |f| %>
<%= f.hidden_field :extraparam, :value => "22" %>
<% end %>


but lets say :extraparam isn't part of the post model..

I have an unknown attribute error in the create method of the controller when I try this, any ideas?

(I want to use the param value itself in the controller for some extra logic)

Answer

Call hidden_field_tag directly. See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag

These helpers exist for all the major form field types, and are handy when you need to go beyond your model's methods.

Comments