mswiszcz
mswiszcz

Reputation: 1006

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

Upvotes: 17

Views: 23365

Answers (4)

The Whiz of Oz
The Whiz of Oz

Reputation: 7043

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

Upvotes: 4

penner
penner

Reputation: 2737

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]

Upvotes: 13

schpet
schpet

Reputation: 10620

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

:checked => 1

Upvotes: 5

Domness
Domness

Reputation: 7605

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

:checked => @manufacture_types[0]

Or

:checked => ManufactureType.first

Upvotes: 46

Related Questions