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