Rails - Default selected radiobutton in SimpleForm :collection

21,876

Solution 1

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

:checked => @manufacture_types[0]

Or

:checked => ManufactureType.first

Solution 2

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]

Solution 3

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

:checked => 1

Solution 4

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
Share:
21,876
mswiszcz
Author by

mswiszcz

Updated on January 26, 2020

Comments

  • mswiszcz
    mswiszcz over 4 years

    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