2012-03-13 108 views
1

我遇到了ActiveResource的一个问题,它已经resolved,并试图将猴子补丁到我的应用程序没有多少运气。猴子补丁ActiveResource ::错误

我已在配置/初始化文件/包含以下内容:

class ActiveResource::Errors < ActiveModel::Errors 
    # https://github.com/rails/rails/commit/b09b2a8401c18d1efff21b3919ac280470a6eb8b 
    def from_hash(messages, save_cache = false) 
     clear unless save_cache 

     messages.each do |(key,errors)| 
     errors.each do |error| 
      if @base.attributes.keys.include?(key) 
      add key, error 
      elsif key == 'base' 
      self[:base] << error 
      else 
      # reporting an error on an attribute not in attributes 
      # format and add themActive to base 
      self[:base] << "#{key.humanize} #{error}" 
      end 
     end 
     end 
    end 

    # Grabs errors from a json response. 
    def from_json(json, save_cache = false) 
     decoded = ActiveSupport::JSON.decode(json) || {} rescue {} 
     if decoded.kind_of?(Hash) && (decoded.has_key?('errors') || decoded.empty?) 
     errors = decoded['errors'] || {} 
     if errors.kind_of?(Array) 
      # 3.2.1-style with array of strings 
      ActiveSupport::Deprecation.warn('Returning errors as an array of strings is deprecated.') 
      from_array errors, save_cache 
     else 
      # 3.2.2+ style 
      from_hash errors, save_cache 
     end 
     else 
     # <3.2-style respond_with - lacks 'errors' key 
     ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') 
     from_hash decoded, save_cache 
     end 
    end 
end 

但似乎仍有待调用activeresource-3.2.2/lib/active_resource/validations.rb:31:in 'from_json'。任何帮助如何正确地猴子补丁这将非常感激。

谢谢!

回答

7

事实证明,问题是我的文件在配置中加载后,Rails延迟加载ActiveResource,用原始定义覆盖它。在定义修补代码之前,修复程序只需要所需的文件。

我修改后的代码:

require 'active_resource/base' 
require 'active_resource/validations' 

module ActiveResource 
    class Errors 
    # https://github.com/rails/rails/commit/b09b2a8401c18d1efff21b3919ac280470a6eb8b 

    def from_hash(messages, save_cache = false) 
     clear unless save_cache 

     messages.each do |(key,errors)| 
     errors.each do |error| 
      if @base.attributes.keys.include?(key) 
      add key, error 
      elsif key == 'base' 
      self[:base] << error 
      else 
      # reporting an error on an attribute not in attributes 
      # format and add themActive to base 
      self[:base] << "#{key.humanize} #{error}" 
      end 
     end 
     end 
    end 

    # Grabs errors from a json response. 
    def from_json(json, save_cache = false) 
     decoded = ActiveSupport::JSON.decode(json) || {} rescue {} 
     if decoded.kind_of?(Hash) && (decoded.has_key?('errors') || decoded.empty?) 
     errors = decoded['errors'] || {} 
     if errors.kind_of?(Array) 
      # 3.2.1-style with array of strings 
      ActiveSupport::Deprecation.warn('Returning errors as an array of strings is deprecated.') 
      from_array errors, save_cache 
     else 
      # 3.2.2+ style 
      from_hash errors, save_cache 
     end 
     else 
     # <3.2-style respond_with - lacks 'errors' key 
     ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') 
     from_hash decoded, save_cache 
     end 
    end 
    end 
end 
+0

非常感谢。我很高兴终于找到解决办法。 – 2014-02-05 10:29:02