rico_mac
rico_mac

Reputation: 888

checkbox returning true or false in rails

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

Upvotes: 0

Views: 1582

Answers (1)

user419017
user419017

Reputation:

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.

Upvotes: 1

Related Questions