Meaning that, in PostsController, def create, I want to access the value that is passed in the column for the boolean value agree. When creating a post, there is a checkbox that sets this value. Checked makes agree true (1) and unchecked makes agree false (0). Right now I have the code params[:post][:agree]
in order to access this value, but that doesn't seem to work.
When I try to use that in an if statement, the statement always occurs, as if params[:post][:agree] always evaluates to true.
Help! Why doesn't this work??
:EDIT:
Post.rb (Post Model)
attr_accessor :agree
attr_accessible :agree
PostController (In def create)
@post.title = "AGREED!!" if params开发者_如何学编程[:post][:agree] == "1"
In Ruby, neither the number 0
nor the string "0"
evaluates to false
in a boolean context. Try if (params[:post][:agree]=="1")
Rails, by default creates both a checkbox (with value="1"
) and a hidden input element (with value="0"
), so you always get a value passed back to the server, which personally I think is silly and override it. Try checking if params[:post][:agree].blank?
instead.
I'm not entirely sure I understand your entire question though. Were you only talking about the form data, or are you talking about your models?
精彩评论