views:

21

answers:

1

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.

+1  A: 

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 %>
tsdbrown
Yes, I am using error_messages_for. However, I also have my own definition of the validate method, so I could write my own error messages. I think your suggestion is a good one, which is (if I am correct) is to create a helper which will build a string of all the error messages by using error_message_on, correct? This way I will be able to specify where the errors show up. Thanks
iHeartDucks
Yeah that would be one way. I would consider doing away with all the error messages at the top and just place each error nicely next to each input. Then at the top you could say please correct the errors below unless @recipe.valid?
tsdbrown
Thanks, I will try it out.
iHeartDucks