ruby-on-rails - ActionView::Template::Error(未定义方法 `silence' 为)

标签 ruby-on-rails ruby heroku kaminari

我在 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

最佳答案

在 Rails5 中,silence 方法已从 ::Logger 基类中删除(参见 issue)

因此,要传递一个 ::Logger 实例,请尝试传递一个公开 silence 方法的 ActiveSupport::Logger 实例(参见documentation ),像这样:

config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))

注意: ActiveSupport::Logger 继承自 ::Logger 基类并包含 LoggerSilence 模块(参见 documentation )

来 self 的 rails 控制台的示例(rails5 和 ruby​​2.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

关于ruby-on-rails - ActionView::Template::Error(未定义方法 `silence' 为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360000/

相关文章:

ruby-on-rails - 获取当前时间的单元测试代码

node.js - NextJS 在 '.next' 目录中找不到有效的构建

node.js - Heroku 上的 mongodb (mongolab)。 'LoopBack connector "mongodb“未安装”

JQuery UI 按钮图标不显示

ruby-on-rails - will_paginate JSON 支持?

ruby-on-rails - 带 rails 的 Redis。 fork错误后需要重新连接Redis

ruby-on-rails - 在 Ruby 中将最大哈希键值插入数组?

ruby-on-rails - Ruby - 向哈希添加值会添加重复数据

javascript - Heroku 上部署的 Node.js + Express 应用程序中找不到 jQuery 文件

ruby-on-rails - Rails - 需要 current_user 信息的自定义验证