开发者

error messages displaying in wrong order when using accepts_nested_attributes_for

开发者 https://www.devze.com 2022-12-19 23:43 出处:网络
I guess I am running into a lot of issues related to error messages, another one. I have the following in my model

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.


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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消