I search a solution to require several selections of response (3), whitout "check_must_be_3" I do not have problem. If I add this solution, I have a rollback. This is my idea but it seems not to be working!
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
html:
<%= 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 %>
controller:
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