I'm getting great, inline error messages for all the other validations in this form, but no matter what I try, I can't seem to get any messages to show up after hitting submit for these checkboxes. The validation is working, as the form won't submit until I check a box, but I'd like to see some feedback for the user.
My models (stripped down):
class Resource < ActiveRecord::Base
attr_accessible :objective_ids
has_many :obsources, :dependent => :destroy
has_many :objectives, :through => :obsources
validates_presence_of :objective_ids, :message => "Must check at least one"
end
class Objective < ActiveRecord::Base
has_many :obsources, :dependent => :destroy
has_many :resources, :through => :obsources
end
The form in my view (stripped down):
<%= simple_form_for @resource, remote: true, :validate => true, :html => { :id => "resource-form#{@resource.skill_id}" } do |f| %>
<%= f.hidden_field :skill_id, :value => @resource.skill_id %>
<%= f.association :objectives, :label => "Objectives Targeted (at least 1)", :collection => Skill.find("#{@resource.skill_id}").objectives, :as => :check_boxes, :label_method => lambda { |objective| "#{objective.content}" } %>
<%= f.submit %>
<% end %>
Thoughts would be much appreciated!
And...I figured it out - sort of. I still can't get client_side_validations to throw me errors for my checkboxes, but I can get errors to show up when I try to submit the form by having my controller render the same create.js.erb, whether it could successfully save or not, then bring up the error messages in there, like this:
<% if @resource.errors.any? -%>
$('#objective-errors<%= @resource.skill_id %>').empty();
$('#objective-errors<%= @resource.skill_id %>').prepend('<%= j render("errors") %>');
<% else %>
// Whatever you want to happen on successful save
<% end %>
I also added an empty span with the objective-errors id right after the checkboxes, so that the errors show up inline, like all the other validation errors. This is bit messy, but happens to work because client_side_validations is making sure that everything else is validated before a save is even attempted, so the only errors ever returned after an unsuccessful save happen to be for my objectives.
So until I want to add another set of checkboxes, this works great! (Though if I ever do, it shouldn't be too difficult to filter the messages to show up next to the appropriate lists of checkboxes.)