2014-10-27 173 views
1

的存在,我有以下形式在它下面的模式: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的存在時遇到問題。我想確保用戶選擇一個或兩個選項。感謝您的幫助

回答

2

您可以嘗試

接受:真

在你的模型。

validates :interest, acceptance: true 
+0

我認爲這工作最初,因爲它拋出一個錯誤時,兩個選項都留空,但我似乎無法得到它在選擇任一或兩者時驗證。 – apardes 2014-10-30 15:18:08