Ruby on Rails验证复选框的存在

时间:2014-10-27 03:30:46

标签: ruby-on-rails ruby-on-rails-4 haml

我在下面的模型中有以下表格:

    = simple_form_for @mem, :url => signup_path, :html => {:id => 'signup_form'}, :method => :post do |f|

        = f.input :interest, label: "I would like to learn more about:", collection: [ ['Interest 1', 'Interest 1' ], ['Interest 2', 'Interest 2']], :as => :check_boxes
        %br
        = f.input :full_name, :as => :string, required: true
        = f.input :email, :as => :string, required: true
        = f.input :telephone, :as => :string, required: true

        = f.submit "Submit"

型号:

class User
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming
    attr_accessor :full_name, :email, :telephone, :interest
    validates :interest, presence: { :message => "Please select your interest."}

我能够成功验证表单中的其他3个字段,但是我无法验证复选框字段:interest的存在。我想确保用户选择其中一个或两个选项。谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

你可以尝试

  

接受:是的

在你的模特中。

validates :interest, acceptance: true