2015-03-02 71 views
1

如何在rails 4中解决此问题。我将项目从rails 3升级到rails 4.未定义的方法`encoding_aware?'

我的服务器控制台正在向我抛出错误消息。无法弄清楚。

 undefined method `encoding_aware?' for #<String:0x000000053e6ca0> 

代码:

module ActionView 
    class Template 
    module Handlers 
    class ERB 
     def call(template) 
     if template.source.encoding_aware? # here is the error 

错误日志:

NoMethodError - undefined method `encoding_aware?' for #<String:0x00000004b05348>: 
    config/initializers/10-patches.rb:63:in `call' 
    actionview (4.2.0) lib/action_view/template.rb:270:in `compile' 
    actionview (4.2.0) lib/action_view/template.rb:245:in `block (2 levels) in compile!' 
    activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument' 
    actionview (4.2.0) lib/action_view/template.rb:333:in `instrument' 
    actionview (4.2.0) lib/action_view/template.rb:244:in `block in compile!' 
    <internal:prelude>:10:in `synchronize' 
    actionview (4.2.0) lib/action_view/template.rb:232:in `compile!' 
    actionview (4.2.0) lib/action_view/template.rb:144:in `block in render' 
    activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument' 
    activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument' 
    actionview (4.2.0) lib/action_view/template.rb:333:in `instrument' 
    actionview (4.2.0) lib/action_view/template.rb:143:in `render' 
    rack-mini-profiler (0.9.1) lib/mini_profiler/profiling_methods.rb:108:in `block in profile_method' 
    actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template' 
    actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument' 
    activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument' 
    activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument' 
    actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument' 
    actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template' 
    actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout' 
    actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template' 
    actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render' 
    actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template' 
    actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render' 
    actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template' 
    actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template' 
    actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body' 

我的配置/初始化/ 10 patchers.rb文件 需要 'active_record'

module ActiveRecord 
    class Base 
    include Redmine::I18n 
    # Translate attribute names for validation errors display 
    def self.human_attribute_name(attr, *args) 
     attr = attr.to_s.sub(/_id$/, '') 

     l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr]) 
    end 
    end 

    # Undefines private Kernel#open method to allow using `open` scopes in models. 
    # See Defect #11545 (http://www.redmine.org/issues/11545) for details. 
    class Base 
    class << self 
     undef open 
    end 
    end 
    class Relation ; undef open ; end 
end 

module ActionView 
    module Helpers 
    module DateHelper 
     # distance_of_time_in_words breaks when difference is greater than 30 years 
     def distance_of_date_in_words(from_date, to_date = 0, options = {}) 
     from_date = from_date.to_date if from_date.respond_to?(:to_date) 
     to_date = to_date.to_date if to_date.respond_to?(:to_date) 
     distance_in_days = (to_date - from_date).abs 

     I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale| 
      case distance_in_days 
      when 0..60  then locale.t :x_days,    :count => distance_in_days.round 
      when 61..720 then locale.t :about_x_months,  :count => (distance_in_days/30).round 
      else    locale.t :over_x_years,  :count => (distance_in_days/365).floor 
      end 
     end 
     end 
    end 
    end 

    class Resolver 
    def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[]) 
     cached(key, [name, prefix, partial], details, locals) do 
     if details[:formats] & [:xml, :json] 
      details = details.dup 
      details[:formats] = details[:formats].dup + [:api] 
     end 
     find_templates(name, prefix, partial, details) 
     end 
    end 
    end 
end 

# Do not HTML escape text templates 
module ActionView 
    class Template 
    module Handlers 
     class ERB 
     def call(template) 
      if template.source.encoding_aware? # HERE IS THE ERROR LINE 
      # template.source.encoding_aware? 
      # encoding_aware? method has been deprecated or moved 

      # First, convert to BINARY, so in case the encoding is 
      # wrong, we can still find an encoding tag 
      # (<%# encoding %>) inside the String using a regular 
      # expression 
      template_source = template.source.dup.force_encoding("BINARY") 

      erb = template_source.gsub(ENCODING_TAG, '') 
      encoding = $2 

      erb.force_encoding valid_encoding(template.source.dup, encoding) 

      # Always make sure we return a String in the default_internal 
      erb.encode! 
      else 
      erb = template.source.dup 
      end 

      self.class.erb_implementation.new(
      erb, 
      :trim => (self.class.erb_trim_mode == "-"), 
      :escape => template.identifier =~ /\.text/ # only escape HTML templates 
     ).src 
     end 
     end 
    end 
    end 
end 

ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe } 

require 'mail' 

module DeliveryMethods 
    class AsyncSMTP < ::Mail::SMTP 
    def deliver!(*args) 
     Thread.start do 
     super *args 
     end 
    end 
    end 

    class AsyncSendmail < ::Mail::Sendmail 
    def deliver!(*args) 
     Thread.start do 
     super *args 
     end 
    end 
    end 

    class TmpFile 
    def initialize(*args); end 

    def deliver!(mail) 
     dest_dir = File.join(Rails.root, 'tmp', 'emails') 
     Dir.mkdir(dest_dir) unless File.directory?(dest_dir) 
     File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) } 
    end 
    end 
end 

ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP 
ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail 
ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile 

# Changes how sent emails are logged 
# Rails doesn't log cc and bcc which is misleading when using bcc only (#12090) 
module ActionMailer 
    class LogSubscriber < ActiveSupport::LogSubscriber 
    def deliver(event) 
     recipients = [:to, :cc, :bcc].inject("") do |s, header| 
     r = Array.wrap(event.payload[header]) 
     if r.any? 
      s << "\n #{header}: #{r.join(', ')}" 
     end 
     s 
     end 
     info("\nSent email \"#{event.payload[:subject]}\" (%1.fms)#{recipients}" % event.duration) 
     debug(event.payload[:mail]) 
    end 
    end 
end 

module ActionController 
    module MimeResponds 
    class Collector 
     def api(&block) 
     any(:xml, :json, &block) 
     end 
    end 
    end 
end 

module ActionController 
    class Base 
    # Displays an explicit message instead of a NoMethodError exception 
    # when trying to start Redmine with an old session_store.rb 
    # TODO: remove it in a later version 
    def self.session=(*args) 
     $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" + 
     "Setting the session secret with ActionController.session= is no longer supported in Rails 3." 
     exit 1 
    end 
    end 
end 
+0

你可以通过错误回溯和发布的代码引发此错误的确切行? – shivam 2015-03-02 05:46:57

+0

嘿Shivam。我用错误日志和代码更新了我的问题,请帮我解决这个问题? 谢谢 – Packer 2015-03-02 05:50:56

+1

什么是'config/initializers/10-patches.rb'?请向我们展示整个文件。这似乎是导致这个问题的原因。 – 2015-03-02 05:57:10

回答

2

encoding_aware?deprecated in Rails 4

如果你快速浏览一下在Rails 3 sourceString类,你可以找到实现:

def encoding_aware? 
    true 
end 

因此,要解决您的问题,您可以简单地通过扩展String类中定义缺少方法,或者找并用true替换encoding_aware?的任何调用实例。

这是扩展是什么样子:

class String 
    def encoding_aware? 
    true 
    end 
end 
+0

Mike Slutsky!我应该在哪里定义缺失的方法。在哪个文件中? – Packer 2015-03-02 06:21:08

+0

你的'config/initializers'目录中的任何文件都可以工作,你只需要确保它在'encoding_aware?'调用发生之前就被加载了。所以我可能会尝试将它添加到'config/initializers/10-patchers.rb'的顶部,我会将代码块添加到答案中。 – 2015-03-02 06:29:19