16

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

mswiszcz
  • 980
  • 1
  • 8
  • 26
  • 5
    Ok. I've found answer for this question. Simply add :checked => 1 (value) at the end of the line and it works :) – mswiszcz Jun 25 '11 at 13:22

4 Answers4

45

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

:checked => @manufacture_types[0]

Or

:checked => ManufactureType.first
Domness
  • 7,585
  • 8
  • 40
  • 50
  • That's gonna reset the button choice if any of the form fields are invalid and it re-renders. I don't know if it's the best way, but I solved this by just setting the the attribute in my `new` action so `@manufacturer = Manufacturer.new (manufacturer_type_id: 0)` – ohhh Sep 18 '15 at 17:34
13

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]
penner
  • 2,707
  • 1
  • 37
  • 48
5

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

:checked => 1
schpet
  • 9,664
  • 6
  • 32
  • 35
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
The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85