Access error messages for a nested attribute field

I have a form created using a simple_form array that fills 2 models using nested attributes. I want to check if there are any errors and display a new block. However, I'm not sure how to get the error message for the location attribute of the Booking model correctly.

 class Booking < ActiveRecord::Base belongs_to :customer attr_accessible :date_wanted, :location end 

and

 class Customer < ActiveRecord::Base has_many :bookings accepts_nested_attributes_for :bookings attr_accessible :name, :phone, :bookings_attributes validates_presence_of :name, :phone end 

Type of form:

 simple_form_for @customer, {:html => { :class => "form-horizontal" }} do |f| = f.input :name = f.input :phone = f.simple_fields_for :bookings do |b| = b.input :date = b.input :location - if @customer.errors[:appointments_attributes][:location] # insert code if any validation errors for the date field were found = f.button :submit 
+7
source share
1 answer

b is an instance of the form builder by holding booking , so you can try:

 # ... if b.object.errors[:location] # ... 
+7
source

All Articles