0

I am trying to create a form that has an option, which if chosen, via a checkbox, returns true or false to the database. I have added the column to my db like so

rails g migration AddWantSellToPosts want_sell?:boolean

and also added the necessary params to the controllers page so that data is passed through. However no value is being returned to the database. I suspected it was my implementation of the check_box in Rails, however I am still not sure.

this is the html.erb

<%= f.text_field :price, :id => 'price' %>
<%= f.check_box :want_sell? %>

this is the posts_controller.rb

  before_action :authenticate_user!, only: [:new, :create, :destroy]

....

@post = Post.new params[:post].permit(:description, :picture, :tag_names, :price, :want_sell?)
@post.user = current_user

I should add i am new to rails and coding in general so I may be missing something really obvious!

Thanks

1
  • Rename the column to want_sell, want_sell? is a dynamic method that Rails will make available to check if the boolean is true/false. Also, are you saving the post? I see @post = Post.new but no @post.save. Commented Apr 19, 2014 at 13:28

1 Answer 1

1

As in my comment...

Rename the column to want_sell, want_sell? is a dynamic method that Rails will make available to check if the boolean is true/false. Also be sure to call @post.save.

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

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.