Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 400
Elliot Avatar asked May 14 '11 05:05

Elliot


2 Answers

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.

like image 158
Paul Rosania Avatar answered Nov 17 '22 09:11

Paul Rosania


The following worked for me in passing extra parameters from the view back to the controller that were a part of my model and not part of my model.

    <%= hidden_field_tag :extraparam, value %>

Example usage

<%= hidden_field_tag :name, "John Smith" %>
like image 4
Conor Avatar answered Nov 17 '22 10:11

Conor