How to set the default option with Rails collection_radio_buttons?

So, I'm trying to set the first radio button, by default, set for the form. I canโ€™t figure out how to do this. I can get all the input to have checked="checked" by adding check: true or checked: "checked" in the html parameters as shown below:

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

I tried to specify check checked: @category.first , as shown below:

 <%= 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?

+7
ruby-on-rails
source share
2 answers

I think you should mark id for verification, so change to { checked: @category.first.id }

+12
source share

It is written for me that in the first branches {}, this way:

 form.collection_radio_buttons(:category_id, @categories, :id, :name, { checked: @categories.first.id }, { class: "category-options"}) 
0
source share

All Articles