Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 在rails中添加if验证_Ruby On Rails_Ruby On Rails 4_Simple Form_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 在rails中添加if验证

Ruby on rails 在rails中添加if验证,ruby-on-rails,ruby-on-rails-4,simple-form,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 4,Simple Form,Ruby On Rails 5,我搜索一个需要多个响应选项(3)的解决方案, 没有“检查必须是3”我没有问题。 如果我添加此解决方案,我将回滚。 这是我的想法,但似乎不起作用 model.rb: validates :check, presence: true validate :check_must_be_3 private def checks_must_be_3 if !check != 3 errors[:base] << "You must select exac

我搜索一个需要多个响应选项(3)的解决方案, 没有“检查必须是3”我没有问题。 如果我添加此解决方案,我将回滚。 这是我的想法,但似乎不起作用

model.rb:

validates :check, presence: true
validate :check_must_be_3

  private
    def checks_must_be_3
      if !check != 3
        errors[:base] << "You must select exactly 3 checks"
      end
    end

谢谢,我有这个错误:参数的数目错误(给定0,预期为1+)
<%= simple_form_for @answer do |f| %>
  <h3>Choose 3 answers</h3>
    <ul>
    <% (1..5).each do |x| %>
      <div class="checkbox">
        <label>
          <input type="checkbox" name="answer[check][]" id="optionsCheckbox<%= x %>" value="<%= x %>" />
          <%= x %>
        </label>
      </div>
    <% end %>
    <%= f.button :submit, "Submit", class: "btn btn-primary" %>
<% end %>
private

def answer_params
    params.require(:answer).permit(check:[])
end
#try this
    private
    def checks_must_be_3
      unless check.count == 3
        errors.add(:base , "You must select exactly 3 checks")
      end
    end