Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Default selected radiobutton in SimpleForm :collection

I've litte problem with radiobuttons in SimpleForm.

When i use

= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio

Rails simply generates few radiobuttons, but none of them are selected. I want first radiobutton to be selected by default. How can i make it?

Thanks

like image 210
mswiszcz Avatar asked Jun 25 '11 13:06

mswiszcz


4 Answers

If you pass in the manufacture types into the view, you can do the following:

:checked => @manufacture_types[0]

Or

:checked => ManufactureType.first
like image 151
Domness Avatar answered Nov 10 '22 00:11

Domness


My example was slightly more complicated, none of the other answers worked for me since there was no collection or model to reference.

= f.input :attending, as: :radio_buttons, :collection => [ ['Yes', true], ['No', false] ], :checked => ['Yes', true]
like image 20
penner Avatar answered Nov 10 '22 00:11

penner


from op's comment, adding this parameter worked for me:

:checked => 1
like image 5
schpet Avatar answered Nov 09 '22 23:11

schpet


Here is an excerpt of my code which works:

= f.input :body_format,
  collection: [['markdown', 'Markdown']],
  label_method: :last,
  value_method: :first,
  as: :radio_buttons,
  checked: 'markdown', # THIS
  required: true
like image 4
The Whiz of Oz Avatar answered Nov 09 '22 22:11

The Whiz of Oz