Getting nicely formated error page for associated models

I have a question regarding reporting of errors for models which
contain other models.

For example, if I have a User model which contains a Comments
collection, and I am not able to see any errors encountered when
validating the comments.

So if I do something like the following to append a new comment:

user = User.find(id)

comment = Comment.new(:comment => params[:comment_text],
:user_id => id)

user.comments << comment
user.save!

When I do this, with an empty :comment_text string, I get an exception
(coming from a validates_length_if associated with the Comment
model).

My question is, how can I get this to show on the form via <%=
error_messages_for… %>, nicely formated instead of an application
exception error message?

At the moment I have a <%= error_messages_for :user %>, which results
in a nicely formated page showing any validation errors for User. But
not if an empty comment is added to the user’s comments collection.

Any help would be greatly appreciated.

Cheers,
Diego

Hi Diego,

take a look at http://railsforum.com/viewtopic.php?id=717 , that should
provide a solution to your problem.

Kind regards,

Nick S.

http://www.railshostinginfo.com
Compare Rails hosting companies

Nick, thanks for the reply. Appreciate it.

On Feb 16, 6:18 pm, Nick S. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs