Forum: Ruby on Rails error_messages_for when saving within another model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b87f1ad86f6a44241832314873e8d54?d=identicon&s=25 Ryan Lundie (Guest)
on 2006-05-03 16:20
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!
4ae3c0b56fcf66f8383ecb73cc8d956e?d=identicon&s=25 Christopher Winslett (Guest)
on 2006-05-03 18:34
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 Lundie 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!
This topic is locked and can not be replied to.