rails simple_form - hidden field - create?
Asked Answered
S

5

183

How can you have a hidden field with simple form?

The following code:

= simple_form_for @movie do |f|
  = f.hidden :title, "some value"
  = f.button :submit

results in this error:

undefined method `hidden' for #SimpleForm::FormBuilder:0x000001042b7cd0
Suffolk answered 20/3, 2011 at 21:3 Comment(0)
S
329

try this

= f.input :title, :as => :hidden, :input_html => { :value => "some value" }
Substance answered 20/3, 2011 at 21:7 Comment(2)
Thanks, that worked. = f.input :title, :as => :hidden, :input_html => { :value => "some value" }Suffolk
This is the simple_form way to do hidden inputs, however, if only a hidden input is needed, then just use Rails' hidden_field form builder since Simple Form inherits all the form builder methods.Amadou
P
291

Shortest Yet !!!

=f.hidden_field :title, :value => "some value"

Shorter, DRYer and perhaps more obvious.

Of course with ruby 1.9 and the new hash format we can go 3 characters shorter with...

=f.hidden_field :title, value: "some value"
Profanity answered 5/9, 2011 at 0:30 Comment(3)
Or <%= f.hidden_field :term_id, :value => @transaction.term_id %>Dram
This should be the accepted answer. Even though the question asked about simple form, there is no reason to use it when this accomplishes the exact same thing with a shorter syntax.Orta
when using custom name or using the input_html key, this did not seem to work as expected. The accepted answer worked betterBaggywrinkle
G
5

Correct way (if you are not trying to reset the value of the hidden_field input) is:

f.hidden_field :method, :value => value_of_the_hidden_field_as_it_comes_through_in_your_form

Where :method is the method that when called on the object results in the value you want

So following the example above:

= simple_form_for @movie do |f|
  = f.hidden :title, "some value"
  = f.button :submit

The code used in the example will reset the value (:title) of @movie being passed by the form. If you need to access the value (:title) of a movie, instead of resetting it, do this:

= simple_form_for @movie do |f|
  = f.hidden :title, :value => params[:movie][:title]
  = f.button :submit

Again only use my answer is you do not want to reset the value submitted by the user.

I hope this makes sense.

Greyso answered 4/8, 2014 at 14:15 Comment(1)
For simple_form_for, the hidden method using here is going to raise an error undefined method hidden' for #<SimpleForm::FormBuilder:0x00007ffa6cde0be8>`Bisk
M
4
= f.input_field :title, as: :hidden, value: "some value"

Is also an option. Note, however, that it skips any wrapper defined for your form builder.

Menhaden answered 7/1, 2014 at 21:49 Comment(0)
V
0

None of the above worked perfectly in my case, as they were leaving a blank rectangle on my frontend. What worked for me was -

<%= f.text_field :title, value: "some value", type: "hidden" %>

Vaudois answered 24/5, 2022 at 12:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.