Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preselect Check box with Rails Simple_form

I'm using Simple_Form with Rails 3 and it's great. I have a simple question. I can create a check box using f.input if the type is boolean behind the scenes. However, I would like it to be preselected as true.

Is there a way to do this via the view?

like image 682
Drew Avatar asked Nov 07 '10 03:11

Drew


2 Answers

If you don't want to, or cannot update your migration to do 'PerfectlyNormal's answer, then you can add the following to the end of your f.input:

:input_html => { :checked => true } 

So, it would become:

= f.input :some_flag, :input_html => { :checked => true } 

Hope that helps!

like image 151
Gav Avatar answered Sep 22 '22 10:09

Gav


the best way would be to set the default value in your model, something like

create_table :my_table do |t|   ...   t.boolean :my_boolean, :default => true   ... end 

which simple_form will pick up on, and set the checkbox to be checked by default.

A simpler way is to do it as Dave says, and just force the value to 1, but if a validation fails, and you display the form again, it will still be checked, regardless of how it looked when the form was submitted.

like image 31
PerfectlyNormal Avatar answered Sep 25 '22 10:09

PerfectlyNormal