# Observe\_field for radio\_button

**URL:** <https://www.ruby-forum.com/t/observe-field-for-radio-button/101445>\
**Category:** Rails\
**Created:** [August 4, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/observe-field-for-radio-button/101445 "2006-08-04T21:30:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 4, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/observe-field-for-radio-button/101445/1 "2006-08-04T21:30:01Z")

</div>

Hi,

I need to observe a set of radio buttons but am having great trouble. My  
observe\_field looks like this:

\<%= observe\_field :user\_select, :frequency =\> 0, :url =\> { :action =\>  
:admin\_control } %\>

Now it works fine if I use a text\_field like this:

\<%= text\_field :user, :select, “size” =\> 20, “maxlength” =\> 16 %\>

But I want a radio button, and what I have is this:

\<%= radio\_button(“user”, “select”, user.id) %\>

The above doesn’t work and neither does:

\<%= radio\_button\_tag(“admin”, “select”, user.id) %\>

* * *

I ahve no clue why the radio buttons don’t work with the observer. Any  
help would be greatly appreciated!

Thanks,

Aditya

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 5, 2006, 3:25am UTC](https://www.ruby-forum.com/t/observe-field-for-radio-button/101445/2 "2006-08-05T03:25:45Z")

</div>

Aditya R. wrote:

> \<%= radio\_button\_tag(“admin”, “select”, user.id) %\>  
> The current API docs have not been updated to reflect the fact that the  
> radio button helper creates an input with an id of object\_method\_value  
> rather than just object\_method. (Remember: “View source” is your  
> friend.)

So you want:

\<%= observe\_field ‘user\_select\_’ + @user.id, … } %\>

(You also want user to be an instance variable for the radio button  
helper to create an appropriately-selected input.)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 5, 2006, 4:49am UTC](https://www.ruby-forum.com/t/observe-field-for-radio-button/101445/3 "2006-08-05T04:49:09Z")

</div>

Mark,

Thanks for the reply. Actually, I noticed in the page source that Rails  
adds the @user.id part in the ‘id’ of the radio button. I tried your  
solution soon after posting my earlier message, but surprisingly it  
diidn’t work, although it created the same ‘id’ as the text\_field does  
😕

I think I’ve got it to wok by not using the radio\_button method at all  
but simply using the HTML tag like this:

\<INPUT type=“radio” id=“user\_select” name=“user[select]” value=\<%=  
user.id %\> \>

Your solution should work though, don’t know why it doesn’t.

-Aditya

Mark Reginald J. wrote:

> Aditya R. wrote:
> 
> > \<%= radio\_button\_tag(“admin”, “select”, user.id) %\>  
> > The current API docs have not been updated to reflect the fact that the  
> > radio button helper creates an input with an id of object\_method\_value  
> > rather than just object\_method. (Remember: “View source” is your  
> > friend.)
> 
> So you want:
> 
> \<%= observe\_field ‘user\_select\_’ + @user.id, … } %\>
> 
> (You also want user to be an instance variable for the radio button  
> helper to create an appropriately-selected input.)
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.
