导轨validates_associated与模型的错误信息 [英] Rails validates_associated with model's error message

查看:197
本文介绍了导轨validates_associated与模型的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是在模型validates_associated使用其他型号的验证code。这个问题是一个失败的验证信息是无效的..是。

我要泡实际的描述性错误形成模型验证未能顶!

我发现这个问题:
<一href=\"http://stackoverflow.com/questions/7387459/validates-associated-with-models-error-message\">validates与模型的错误信息

相关

其中有什么看起来像一个非常紧密的解决方案:

 模块的ActiveRecord
  模块验证
    类AssociatedBubblingValidator&LT; ::加载ActiveModel EachValidator
      高清validate_each(记录,属性值)
        (value.is_a(阵列)值:?[值])。每做| V |
          除非v.valid?
            v.errors.full_messages.each做|味精|
              record.errors.add(属性,味精,options.merge(:价值=&GT;值))
            结束
          结束
        结束
      结束
    结束    模块ClassMethods
      高清validates_associated_bubbling(* attr_names)
        validates_with AssociatedBubblingValidator,_merge_attributes(attr_names)
      结束
    结束
  结束
结束

但它实际上击中了一个错误:

 未定义的方法`有效吗?为#&LT; TicketType :: ActiveRecord_Associations_CollectionProxy:0x007ff86474a478&GT;

任何人可以请帮助各地完成这项工作几乎工作!?

完整的错误轨迹是:

 未定义的方法`有效吗?为#&LT; TicketType :: ActiveRecord_Associations_CollectionProxy:0x007ff8646ba148&GT;提取的源(大约6号线):
4

6
7
8
9      高清validate_each(记录,属性值)
        (value.is_a(阵列)值:?[值])。每做| V |
          除非v.valid?
            v.errors.full_messages.each做|味精|
              record.errors.add(属性,味精,options.merge(:价值=&GT;值))
            结束Rails.root:/用户/ andyarmstrong /文件/个人/ clazzoo_main应用程序跟踪|框架跟踪|完整曲线
配置/初始化/ associated_bubbling_validator.rb:6:`在validate_each块
配置/初始化/ associated_bubbling_validator.rb:5:'每个'
配置/初始化/ associated_bubbling_validator.rb:5:`validate_each
应用程序/控制器/ events_controller.rb:158:在`块打造


解决方案

实际上没有一个阵列,但一个的ActiveRecord ::协会:: CollectionProxy

所以...

value.is_a?(阵列)?值:[值] #=> [值]

  [值]。每做| V |
  除非v.valid?
    #......
  结束
结束

将提高该错误

 未定义的方法`有效吗?为#&LT; TicketType :: ActiveRecord_Associations_CollectionProxy:0x007ff86474a478&GT;

您可以试试这个:

 模块的ActiveRecord
  模块验证
    类AssociatedBubblingValidator&LT; ::加载ActiveModel EachValidator
      高清validate_each(记录,属性值)
        ((value.kind_of(可枚举)|| value.kind_of(ActiveRecord的::关系))值:???[值])。每做| V |
          除非v.valid?
            v.errors.full_messages.each做|味精|
              record.errors.add(属性,味精,options.merge(:价值=&GT;值))
            结束
          结束
        结束
      结束
    结束    模块ClassMethods
      高清validates_associated_bubbling(* attr_names)
        validates_with AssociatedBubblingValidator,_merge_attributes(attr_names)
      结束
    结束
  结束
结束

I am using the validates_associated in a model to use the other model's validation code. The problem with this is the message for a failed validation is "..is invalid".

I want to bubble the actual descriptive error form the model validation's failure to the top!

I found this question: validates associated with model's error message

Which had what looks like a very close solution:

module ActiveRecord
  module Validations
    class AssociatedBubblingValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        (value.is_a?(Array) ? value : [value]).each do |v|
          unless v.valid?
            v.errors.full_messages.each do |msg|
              record.errors.add(attribute, msg, options.merge(:value => value))
            end
          end
        end
      end
    end

    module ClassMethods
      def validates_associated_bubbling(*attr_names)
        validates_with AssociatedBubblingValidator, _merge_attributes(attr_names)
      end
    end
  end
end

However it actually hits an error:

undefined method `valid?' for #<TicketType::ActiveRecord_Associations_CollectionProxy:0x007ff86474a478>

Can anybody please help complete this almost working work around!?

The complete error trace is:

undefined method `valid?' for #<TicketType::ActiveRecord_Associations_CollectionProxy:0x007ff8646ba148>

Extracted source (around line #6):
4
5
6
7
8
9

      def validate_each(record, attribute, value)
        (value.is_a?(Array) ? value : [value]).each do |v|
          unless v.valid?
            v.errors.full_messages.each do |msg|
              record.errors.add(attribute, msg, options.merge(:value => value))
            end

Rails.root: /Users/andyarmstrong/Documents/Personal/clazzoo_main

Application Trace | Framework Trace | Full Trace
config/initializers/associated_bubbling_validator.rb:6:in `block in validate_each'
config/initializers/associated_bubbling_validator.rb:5:in `each'
config/initializers/associated_bubbling_validator.rb:5:in `validate_each'
app/controllers/events_controller.rb:158:in `block in create'

解决方案

value is actually not an Array, but an ActiveRecord::Associations::CollectionProxy.

So...

value.is_a?(Array) ? value : [value] #=> [value]

and

[value].each do |v|
  unless v.valid?
    # ......
  end
end

would raise that error

undefined method `valid?' for #<TicketType::ActiveRecord_Associations_CollectionProxy:0x007ff86474a478>

You can try this:

module ActiveRecord
  module Validations
    class AssociatedBubblingValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        ((value.kind_of?(Enumerable) || value.kind_of?(ActiveRecord::Relation)) ? value : [value]).each do |v|
          unless v.valid?
            v.errors.full_messages.each do |msg|
              record.errors.add(attribute, msg, options.merge(:value => value))
            end
          end
        end
      end
    end

    module ClassMethods
      def validates_associated_bubbling(*attr_names)
        validates_with AssociatedBubblingValidator, _merge_attributes(attr_names)
      end
    end
  end
end

这篇关于导轨validates_associated与模型的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆