Error_messages_for when saving within another model

Hi,

I have this save routine:

def customer_address_create
customer_options
@customer = Customer.find(params[:id])
@customer.addresses.create(params[:address])
@saved = @customer.save
end

That passes to a rjs template like this:

if @saved
page.visual_effect :Fold, ‘add_address’
page.replace_html ‘addresses’, :partial => ‘customer_address_list’
else
page.replace_html ‘address_error’, “#{error_messages_for(‘customer’)}”
page.visual_effect :Highlight, ‘address_error’
page.show ‘address_error’
end

To handle the error messages. I am unable to get a proper error message
however. If i use error_messages_for(‘address’) it returns nothing, if I
use error_messages_for(‘customer’) it returns

There were problems with the following fields:

Addresses is invalid

instead of returning the actual error messages for the address model.

Any idea what i am missing?

Thanks for the help!

Put your validation for the address in the customer model. Reference it
by customer.address for the value, and by symbol for the “errors.add.”

class Customer < ActiveRecord
validates
errors.add(:address, “must be a valid address”) if
customer.address.invalid
end
end

in your rhtml field:

<%= errors_messages_for :customer %>

That should work. It worked for me once.

Ryan L. wrote:

Hi,

I have this save routine:

def customer_address_create
customer_options
@customer = Customer.find(params[:id])
@customer.addresses.create(params[:address])
@saved = @customer.save
end

That passes to a rjs template like this:

if @saved
page.visual_effect :Fold, ‘add_address’
page.replace_html ‘addresses’, :partial => ‘customer_address_list’
else
page.replace_html ‘address_error’, “#{error_messages_for(‘customer’)}”
page.visual_effect :Highlight, ‘address_error’
page.show ‘address_error’
end

To handle the error messages. I am unable to get a proper error message
however. If i use error_messages_for(‘address’) it returns nothing, if I
use error_messages_for(‘customer’) it returns

There were problems with the following fields:

Addresses is invalid

instead of returning the actual error messages for the address model.

Any idea what i am missing?

Thanks for the help!