Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails验证复选框的存在_Ruby On Rails_Ruby On Rails 4_Haml - Fatal编程技术网

Ruby on rails RubyonRails验证复选框的存在

Ruby on rails RubyonRails验证复选框的存在,ruby-on-rails,ruby-on-rails-4,haml,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' ],

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

    = 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
。我希望确保用户选择一个或两个选项。谢谢你的帮助

你可以试试

接受:正确

在你的模型中

validates :interest, acceptance: true

起初我认为这是可行的,因为当两个选项都为空时,它会抛出一个错误,但当选择其中一个或两个选项时,我似乎无法让它进行验证。