Reputation: 319
I have created a small form to allow users submit 'opinions'. This can be positive or negative. I got a 'positive' boolean field on the DB. This should be true if positive or false if negative.
=form_for @opinion do |f|
= f.hidden_field :event_id
%p
=f.radio_button(:positive, :checked => (:positive == 1))
=f.label :true, 'Positive'
=f.radio_button(:positive, :checked => (:positive == 0))
=f.label :false, 'Negative'
%p
=f.label 'Body'
=f.text_area :body
=f.submit
However, everything is stored right but the positive field. What am I doing wrong?
This is what I get
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EgFB9zohTA93ClNAQpS0qcsEivSCe1imjDUuw/e/YYA=", "opinion"=>{"event_id"=>"31", "positive"=>"{:checked=>false}", "body"=>"asdasasdasd"}, "commit"=>"Create Opinion", "locale"=>"en"}
Upvotes: 0
Views: 1460
Reputation: 7616
Rails will add 'checked' when your attribute's value is equal to the supplied value
=f.radio_button :positive, 1
=f.label :true, 'Positive'
=f.radio_button :positive, 0
=f.label :false, 'Negative'
please check the doc
Upvotes: 1
Reputation: 19294
Remove the :checked
stuff. If your database field is a boolean, than rails form_for knows which f.radio_button to check.
Upvotes: 1