ActionView::Template::Error(未定义方法“silence”)

6

我在Heroku上遇到了一个非常奇怪的问题。我有一个如下所示的视图:

= content_for :header_title do
  = t('.header_title')

- if @appointments.exists?
  %table.table.table-striped.table-bordered.table-hover
    %thead
      %tr
        %th= t('.id')
        %th= t('.athena_health_id')
        %th= t('.start_time')
        %th= t('.duration')
        %th= t('.provider')
        %th= t('.created_at')
        %th= t('.updated_at')
    %tbody
      = render @appointments

  = paginate @appointments
- else
  %h3.text-center= t('.appointments_not_found')
%hr/

没有什么特别的。当我访问在Heroku上使用此模板的页面时,会收到以下信息:

 ActionView::Template::Error (undefined method `silence' for #<Logger:0x007f7a86267a70>):

测试已通过,在我的本地一切运作正常,但我不知道出了什么问题。Stacktrace 显示问题在于以下这行代码:

= paginate @appointments

我正在使用Rails 5.0和kaminari(1.0.0.alpha)。有什么想法吗?
编辑: 在我的production.rb文件中:
  if ENV['RAILS_LOG_TO_STDOUT'].present?
    config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
  end

config.log_formatter = ::Logger::Formatter.new

1
错误似乎无法找到名为“silence”的方法,该方法属于一个名为Logger的类。你的代码中是否有Logger类,或者这是在Heroku端出现的问题? - Okomikeruko
不,我的类没有日志记录器类。在production.rb中,我还有: 如果 ENV['RAILS_LOG_TO_STDOUT'].present? config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) endconfig.log_formatter = ::Logger::Formatter.new - Mateusz Urbański
1
你看过Github上的这个问题吗?::Logger类的silence方法自Rails4.2以来已被弃用,并在Rails5中被删除。 - NickGnd
1个回答

12
在Rails5中,::Logger基类中的silence方法被移除(参见此问题)。因此,不要传递::Logger实例,而应该尝试传递一个ActiveSupport::Logger实例,它公开了silence方法(请参见文档),如下所示:config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
注意:ActiveSupport::Logger继承自::Logger基类,并包含LoggerSilence模块(请参见文档)。
以下是我的rails控制台(Rails5和Ruby2.3.0)中的示例。
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
 => #<Logger:0x007f890b8a3d10 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f890b8a2cd0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007f890b8a26e0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007f890b8a2870 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f890b8a2730>>> 
logger.silence
NoMethodError: undefined method `silence' for #<Logger:0x007f890b8a3d10>
# ...

logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
 => #<ActiveSupport::Logger:0x007f890bd2a028 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f890bd29fb0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007f890bd29f10 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007f890bd29f60 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f890bd29f38>>, @local_levels=#<Concurrent::Map:0x007f890bd29ec0 entries=0 default_proc=nil>> 
logger.silence
LocalJumpError: no block given (yield)
# The method exists, but I don't pass any block

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接