merging model validation errors
Monday, May 14th, 2007Here is a simple chunk of code that I find very useful when working with multiple objects in one form that leverage the Rails model validation functions.
In this example we see how to merge validation errors for more then one object so they can be displayed with the error_messages_for helper.
if @user.update_attributes(params[:user])
flash[:notice] = “Account Updated”
else
flash[:notice] = “Account Updated Failed”
@user.profile.errors.each do |attribute, error|
@user.errors.add(attribute, error)
end
end