简体   繁体   English

使用accepts_nested_attributes_for时以错误的顺序显示错误消息

[英]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 在“ RecipeIngredient”模型中,我有这个

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 我正在运行ruby v1.8.7和rails v2.3.5

Thanks. 谢谢。

How are you displaying the error messages, with error_messages_for? 您如何使用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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveModel :: MassAssignmentSecurity :: Error甚至在使用accepts_nested_attributes_for时 - ActiveModel::MassAssignmentSecurity::Error even when using accepts_nested_attributes_for 来自模型的Rails错误消息,是否可以订购它们? 与accepts_nested_attributes_for有关的问题 - Rails error messages from models, is there a way to order them? issues with accepts_nested_attributes_for 使用accepts_nested_attributes_for - using accepts_nested_attributes_for 使用accepts_nested_attributes_for在表单中显示固定数量的子代 - Displaying a fixed number of children in a form using accepts_nested_attributes_for 具有accepts_nested_attributes_for的自定义验证消息 - Custom validation messages with accepts_nested_attributes_for 使用Rails的accepts_nested_attributes_for功能时如何维护子记录的顺序? - How to maintain an order on child records when using Rails' accepts_nested_attributes_for feature? 保存accepts_nested_attributes_for的顺序 - Save order of accepts_nested_attributes_for 订单 accepts_nested_attributes_for 行 - Order accepts_nested_attributes_for lines 在模型中使用accepts_nested_attributes_for时出现Rails 4.1错误 - Rails 4.1 error when use accepts_nested_attributes_for in model 在Ruby和Rails中使用accepts_nested_attributes_for存储时出错 - Error while using accepts_nested_attributes_for store in Ruby and Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM