Radio buttons for boolean field, how to do a “false”?

前端 未结 2 2065
野的像风
野的像风 2020-12-13 10:14

I am currently trying to insert some simple true/false radio buttons in Rails 3, but I can\'t find a way to make a radio button insert \"false\".

My code is the foll

相关标签:
2条回答
  • 2020-12-13 10:36

    This is it:

    http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of

    validates_presence_of() validates that the specified attributes are not blank (as defined by Object#blank?)

    If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false]

    This is due to the way Object#blank? handles boolean values: false.blank? # => true

    I tried your example using a scaffold and "1" and "0" as in

    <%= f.radio_button :foo, "0" %>
    <%= f.radio_button :foo, "1" %>
    

    and they worked.

    0 讨论(0)
  • 2020-12-13 10:48

    I recently came to another solution for this:

    validates_presence_of :accident_free, :if => 'accident_free.nil?'
    

    Explanation here

    0 讨论(0)
提交回复
热议问题