Ayrad
Ayrad

Reputation: 4056

simple_form turn boolean into dropdown with labels

I have a boolean column in my model. Say it's called isType1.

Now in my form, I would like to have a dropdown with two values (type1 if boolean is true, and type2 if boolean is false) instead of a checkbox or radio buttons.

Is that possible?

Right now I am displaying it as radio buttons:

<%= f.input :isType,  :as => :radio, :label => "Type"%>

I would prefer if I had a dropdown where the user could select type1 or type2 without changing the model to a string instead of a boolean.

Thanks.

Upvotes: 1

Views: 6514

Answers (3)

Weston Ganger
Weston Ganger

Reputation: 6722

When you are using include_blank: false, You must add disabled: [] to allow the falsy values to not be disabled within the select.

<%= f.input :isType, 
            as: :select, 
            collection: [['Type1',false],['Type2',true]], 
            include_blank: false, 
            disabled: [] %>

Upvotes: 0

Ayrad
Ayrad

Reputation: 4056

This is what I was looking for:

<%= f.input :isType, 
            :as => :select, 
            :collection => [['Type1',false],['Type2',true]], 
            :include_blank => false, 
            :label => "Type" %>

Upvotes: 16

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

Just use select for this:

<%= f.input :isType, :as => :select, :label => "Type"%>

Upvotes: 1

Related Questions