2010-02-15 68 views
0

我想我遇到了很多與錯誤消息相關的問題,另一個問題。使用accept_nested_attributes_for時顯示錯誤順序的錯誤消息

我在我的模型

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 

在「RecipeIngredient」的模式,下面我有這個

class RecipeIngredient < ActiveRecord::Base 
    belongs_to :recipe 

    validates_presence_of :ingredient_id, :serving_size_id, :quantity 

end 

現在,當我看到錯誤消息我看到的錯誤消息配方配料模型第一而不是配方模型。如何首先顯示配方模型的錯誤消息?

我正在運行ruby v1.8.7和rails v2.3.5

謝謝。

回答

1

你如何顯示錯誤消息,error_messages_for?我認爲錯誤存儲在散列中,在這種情況下它沒有保證的順序。你可以推出自己的幫手,或者如何顯示內聯錯誤:

<%= error_message_on @recipe, :title %> 
+0

是的,我正在使用error_messages_for。但是,我也有自己的驗證方法的定義,所以我可以編寫自己的錯誤消息。我認爲你的建議是一個很好的建議,那就是(如果我正確的話)是通過使用error_message_on創建一個幫助程序來構建所有錯誤消息的字符串,那麼正確?這樣我就可以指定出現錯誤的位置。謝謝 – iJK 2010-02-16 15:09:16

+0

是的,這將是一種方式。我會考慮廢除頂部的所有錯誤消息,並將每個錯誤很好地放在每個輸入旁邊。然後在頂部,你可以說請糾正下面的錯誤,除非@ recipe.valid? – tsdbrown 2010-02-16 15:59:28

+0

謝謝,我會試試看。 – iJK 2010-02-16 16:33:14