Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a default checked option with Rails collection_radio_buttons?

So I'm trying to set the first radio button as checked by default for a form. I can't figure out how to do it. I can get all of the inputs to have checked="checked" by adding checked: true or checked: 'checked' in the html options, as in the following:

<%= f.collection_radio_buttons :category_id, @categories, :id, :name, {}, { checked: true } %>
<%= f.collection_radio_buttons :category_id, @categories, :id, :name, {}, { checked: 'checked' } %>

I've tried specifying checked: @category.first, as in the following:

<%= f.collection_radio_buttons :category_id, @categories, :id, :name, {}, { checked: @category.first } %>

But that doesn't work either. None of the inputs are checked.

Any ideas?

like image 612
AdamB Avatar asked Jun 18 '14 03:06

AdamB


1 Answers

I think you should marked id to be checked, so change to { checked: @category.first.id }

like image 124
zishe Avatar answered Oct 23 '22 14:10

zishe