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

前端 未结 2 2076
野的像风
野的像风 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.

提交回复
热议问题