Take the 2-minute tour ×

I have a form created using the simple_form gem which populates 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 correctly access the error message for the location attribute of the Booking model.

class Booking < ActiveRecord::Base
  belongs_to :customer

  attr_accessible :date_wanted, :location


class Customer < ActiveRecord::Base
  has_many :bookings
  accepts_nested_attributes_for :bookings

  attr_accessible :name, :phone, :bookings_attributes

  validates_presence_of :name, :phone

Form view:

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
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

# ...
if b.object.errors[:location]
# ...
share|improve this answer
Thanks! I'm able to see if there are any error messages by using if b.object.errors[:location].empty?. –  dspencer Aug 23 '12 at 18:51

Your Answer


