Good afternoon all
I'm trying to implement a form that dynamically inserts radio buttons
into a form. The user selects options from a drop-down menu. I use
observe_field to watch for a change and call back to render the
appropriate partial tha contains the radio options. All works nicely
but I'm tripped up on one thing:
When rendering a form using form_for how do I link those radio buttons
to the form? In the partial I have:
<%= form.radio_button( :product_id, thumbnail.id, :checked =>
is_checked ) %>
but obviously since the HTML is being rendered via the partial after
the form has been created there's no form object to use to build the
My questions: is there a way to pass the form object? Am I doing this
I'm trying to implement a form that dynamically
inserts radio buttons into a form.
but obviously since the HTML is being rendered via
the partial after the form has been created there's no
form object to use to build the radio_button.
My questions: is there a way to pass the form object?
Am I doing this completely wrong?
No, and yes. form_for is just a convenience for form building. It has nothing to do with how the browser constructs the params hash.
"Also worth noting is that the form_for yields a form_builder object, in this example as f, which emulates the API for the stand-alone FormHelper methods, but without the object name. So instead of text_field :person, :name, you get away with f.text_field :name"
What happens when the visitor clicks the submit button is that the browser looks for valid elements between the form's start and end tag and adds key / value pairs to the hash. The documentation (api.rubyonrails.org) says that about form_for, albeit in a round-about way.
Short answer... don't use form_for syntax in your partial. Just use straight radio_button syntax specifying the params key you want the value to come back in.
Ah! This makes sense. I had it in my mind that the other way was
depricated and that form_for was the preferred method but of course
there can be no 'form' object after the form is created. Manny thanks
for pointing out the path of least resistance Bill.