merging model validation errors

May 14, 2007 | In: ruby

Here 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
Bookmark and Share
blog comments powered by Disqus

Flickr Pics

13th and washington NYC13th and 9th NYC1st st Jersey CityIMG_2813IMG_2814_2IMG_2816IMG_2815Photo_011908_001Photo_122807_001Photo_121107_001