Hi,
I'm using Rails 3.2.3 with SimpleForm 2.0.1 and Twitter Bootstrap.
My Message model has a required association with to_contact:
class Message < ActiveRecord::Base
belongs_to :to_contact, :class_name => "Contact", :inverse_of => :to_contact_messages
validates :to_contact, :presence => true
end
I used SimpleForm to create a select list of contacts:
<%= f.input :to_contact_id, :required => true,
:collection => @message.account.contacts.order(:last_name, :first_name),
:value_method => :id, :label_method => :display_name,
:input_html => { :class => "input-large" } %>
If I leave the select box blank and submit the form, SimpleForm does not apply the "error" formatting and error message to the field. However if I examine object.errors.full_messages, I see that Rails did detect the validation error; it says "To contact can't be blank".
If I change the model to validate the to_contact_id field instead of the association (to_contact), SimpleForm applies the "error" formatting correctly:
validates :to_contact_id, :presence => true
I was under the impression that when writing the validation for the association, I should only reference the association, not the id (see for example
http://stackoverflow.com/a/3755844/550712). Is SimpleForm supposed to display the error message either way? Is it okay to validate to_contact_id instead of to_contact?
Thanks,
Mark