Ruby on Rails使用ActiveRecord错误处理的奇怪行为

时间:2010-04-22 01:12:22

标签: ruby-on-rails ruby activerecord rest error-handling

有人可以解释为什么会这样吗?

mybox:$ ruby script/console
Loading development environment (Rails 2.3.5)
>> foo = Foo.new
=> #<Foo id: nil, customer_id: nil, created_at: nil, updated_at: nil>
>> bar = Bar.new
=> #<Bar id: nil, bundle_id: nil, alias: nil, real: nil, active: true, list_type: 0, body_record_active: false, created_at: nil, updated_at: nil>
>> bar.save
=> false
>> bar.errors.each_full { |msg| puts msg }
Real can't be blank
Real You must supply a valid email
=> ["Real can't be blank", "Real You must supply a valid email"]

到目前为止,这是完美的,这就是我想要阅读的错误信息。现在更多:

>> foo.bars << bar
=> [#<Bar id: nil, bundle_id: nil, alias: nil, real: nil, active: true, list_type: 0, body_record_active: false, created_at: nil, updated_at: nil>]
>> foo.save
=> false
>> foo.errors.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n  <error>Bars is invalid</error>\n</errors>\n"

这是我无法弄清楚的。为什么我得到酒吧无效与上面显示的错误消息,[“真实不能为空白”,“真实,你必须提供有效的电子邮件”]等。

我的控制器只有一个respond_to方法,其中包含以下内容:

 format.xml  { render :xml => @foo.errors, :status => :unprocessable_entity }

如何让这个输出真正的错误消息,以便用户能够深入了解他们做错了什么?如何在控制器中编写渲染方法以显示所有相应的错误消息?

3 个答案:

答案 0 :(得分:1)

我认为你正在使用

validates_associated:你的foo.rb MODEL中的栏

所以它只给出“酒吧无效”

要检查条形图的错误消息,您必须在

中执行以下操作

查看

<%= error_messages_for :foo, :bar %>

控制器

foo.bar.errors.to_xml

&安培;在foo.rb

中跳过“bar is invalid”消息
  def after_validation
    # Skip errors that won't be useful to the end user
    filtered_errors = self.errors.reject{ |err| %w{ bar }.include?(err.first) }
    self.errors.clear
    filtered_errors.each { |err| self.errors.add(*err) }
  end

答案 1 :(得分:0)

这是因为bar的错误存储在bar对象中。要获得这些错误,您必须执行以下操作:

foo.bar.each do |bar|
  bar.errors.each_full { |msg| puts msg }
end

这对我来说很复杂,但我还没有找到将错误列入一个列表的最佳方法(除了自己处理之外)。我理解它背后的原因(因为每个对象应该只知道它自己的错误)。我通常做的是范围ActiveRecord::Errors并创建一个新的each_full_with_associations函数,将它们全部返回。

当您在具有嵌套字段的表单上看到它时,这一切都很有意义。在这种情况下,错误显示正确,一切都很好。

答案 2 :(得分:0)

我们曾经在特定模型中覆盖errors方法,如果我们也需要子对象的错误,那就像那样

class Foo < ActiveRecord::Base

  alias :errors_without_children :errors

  def errors
    self.bars.each do |i|
      i.errors.each_full do |msg|
        errors_without_children.add_to_base msg
      end
    end
    errors_without_children
  end

end

您仍然可以进一步优化它。但是这个已经将所有bar对象的错误消息添加到foo。