0

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"}
2
  • Why don't you just use a simple checkbox? if checked than positive, if not then negative. or whatever Commented Apr 2, 2013 at 18:07
  • I prefer a visual of 2 options for the users to choose. Commented Apr 2, 2013 at 18:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the :checked stuff. If your database field is a boolean, than rails form_for knows which f.radio_button to check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.