radio_button_tag and default checked option

1,094 views
Skip to first unread message

Fernando Perez

unread,
May 11, 2008, 2:54:53 PM5/11/08
to rubyonra...@googlegroups.com
Hi, I can't get my radio button to be checked by default, here is what I
have:

<%= f.radio_button :gender, "male", :checked => 'true' %><p>Male</p>
<%= f.radio_button :gender, "female" %><p>Female</p>
--
Posted via http://www.ruby-forum.com/.

Fernando Perez

unread,
May 11, 2008, 3:17:31 PM5/11/08
to rubyonra...@googlegroups.com

Here is the generated html:

<input type="radio" value="true" name="user[gender]"
id="user_gender_male"/>

Rails is going nuts here, why is Rails generating the id
"user_gender_male"??? That's why everything is going wrong.

Karthi kn

unread,
May 12, 2008, 4:18:02 AM5/12/08
to rubyonra...@googlegroups.com
I think, the way you are using the :checked option may be wrong.

Try with,

:checked => true
:checked => 'checked'

Ar Chron

unread,
May 12, 2008, 8:32:25 AM5/12/08
to rubyonra...@googlegroups.com
Karthi kn wrote:
> I think, the way you are using the :checked option may be wrong.
>
> Try with,
>
> :checked => true
> :checked => 'checked'

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000929

Returns a radio button tag for accessing a specified attribute
(identified by method) on an object assigned to the template (identified
by object). If the current value of method is tag_value the radio button
will be checked. Additional options on the input tag can be passed as a
hash with options.
Examples

# Let's say that @post.category returns "rails":
radio_button("post", "category", "rails")
# => <input type="radio" id="post_category" name="post[category]"
value="rails" checked="checked" />


or just do :checked => 'checked' yourself in the options.

Reply all
Reply to author
Forward
0 new messages