iJK
iJK

Reputation: 4755

error messages displaying in wrong order when using accepts_nested_attributes_for

I guess I am running into a lot of issues related to error messages, another one.

I have the following in my model

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients

  validates_presence_of :title, :message => "Recipe title cannot be left blank"
  validates_presence_of :servingsize, :message => "Please enter a serving size for the recipe"

  accepts_nested_attributes_for :recipe_ingredients

end

In "RecipeIngredient" model I have this

class RecipeIngredient < ActiveRecord::Base
  belongs_to :recipe

  validates_presence_of :ingredient_id, :serving_size_id, :quantity

end

Now when I see the error messages I see error messages for the recipe ingredient model first and not for the recipe model. How can I display error messages for the recipe model first?

I am running ruby v1.8.7 and rails v2.3.5

Thanks.

Upvotes: 0

Views: 295

Answers (1)

tsdbrown
tsdbrown

Reputation: 5058

How are you displaying the error messages, with error_messages_for? I think errors are stored in a hash in which case it has no guaranteed order. You could roll your own helper, or how about display the errors inline:

<%= error_message_on @recipe, :title %>

Upvotes: 1

Related Questions