# Dynamically add radio buttons to forms?

**URL:** <https://www.ruby-forum.com/t/dynamically-add-radio-buttons-to-forms/86953>\
**Category:** Rails\
**Created:** [February 27, 2007, 2:42am UTC](https://www.ruby-forum.com/t/dynamically-add-radio-buttons-to-forms/86953 "2007-02-27T02:42:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Cummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Chris\_Cummer](https://www.ruby-forum.com/u/Chris_Cummer)\
**Post date:** [February 27, 2007, 2:42am UTC](https://www.ruby-forum.com/t/dynamically-add-radio-buttons-to-forms/86953/1 "2007-02-27T02:42:43Z")

</div>

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  
radio\_button.

My questions: is there a way to pass the form object? Am I doing this  
completely wrong?

Many thanks  
Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Cummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Chris\_Cummer](https://www.ruby-forum.com/u/Chris_Cummer)\
**Post date:** [February 27, 2007, 3:30am UTC](https://www.ruby-forum.com/t/dynamically-add-radio-buttons-to-forms/86953/2 "2007-02-27T03:30:46Z")

</div>

Hi Chris,

Chris\_Cummer wrote:

> 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](http://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.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Chris\_Cummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@Chris\_Cummer](https://www.ruby-forum.com/u/Chris_Cummer)\
**Post date:** [February 27, 2007, 4:12am UTC](https://www.ruby-forum.com/t/dynamically-add-radio-buttons-to-forms/86953/3 "2007-02-27T04:12:51Z")

</div>

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.

Cheers  
Chris
