2012-03-13 122 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