Ruby on Rails的验证复选框
问题描述:
的存在,我有以下形式在它下面的模式:Ruby on Rails的验证复选框
= 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
我认为这工作最初,因为它抛出一个错误时,两个选项都留空,但我似乎无法得到它在选择任一或两者时验证。 – apardes 2014-10-30 15:18:08