Reputation: 14798
I need to set a default checked/unchecked values for a checkbox using simple form in rails, let's say i have a User model who has "subscribed" attribute. So on his profile page i want to output a checkbox which will pass "Yes" value if it's checked. So far i have:
= simple_form_for @user do |user|
= user.input :subscribed, as: :boolean, label: false, inline_label: 'Do you want to subscribe?', value: "yes"
But it still shows 1 as a value of checkbox...
Upvotes: 0
Views: 1233
Reputation: 35350
For a boolean attribute like :subscribed
, you really should store the value as a boolean. Storing 'yes'
or 'no'
is not the best idea. My answer will reflect this better practice.
To get a default true
value (to have the box checked by default), you could provide a default value for the :subscribed
attribute in the User
model
after_initialize do
subscribed = true if subscribed.nil?
end
This would cause the checkbox to be checked by default for users who have not explicitly unsubscribed.
Upvotes: 1