验证rails中的多个复选框

时间:2015-07-23 06:01:54

标签: ruby-on-rails validation ruby-on-rails-4 model

我想验证多个复选框到"选择至少一个"
视图:

<label>
<input type="checkbox" name="type_one" class="r_type" id="one" value="PA">one</label>
<label> 
<input type="checkbox" name="type_two" class="r_type" id="two" value="FA">two</label>
<label>
<input type="checkbox" name="type_three" class="r_type" id="three" value="MA">three</label>
<label>
<input type="checkbox" name="type_four" class="r_type" id="four" value="AD">four</label>

型号:

validate :atleast_one_is_checked

  def atleast_one_is_checked
    errors.add(:base, "Select atleast one output format type") unless type_one || type_two || type_three || type_four
  end

我得到undefined method or variable "type_one" error

0 个答案:

没有答案