【问题标题】:Rails dynamic error pages and Devise: RuntimeErrorRails 动态错误页面和设计:RuntimeError
【发布时间】:2017-06-26 12:55:22
【问题描述】:

我阅读并尝试应用 Matt Brictson 在his awesome article 中所说的关于 Rails 上的动态错误页面的内容。 我使用 Devise 对用户进行身份验证,因此我必须将 authenticate_user 放入 errors_controller.rb

skip_before_action :authenticate_user!, :only => [:not_found, :internal_server_error]

但是当我尝试调用 /404 页面时,这给我一个 RuntimeError in Errors#not_found 失败:

can't add a new key into hash during Iteration

编辑:我也发布了application.rb 代码:

require_relative 'boot'

require 'rails/all'

I18n.available_locales = [:en, :de]

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)


module SoundnotationBackend
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified 
here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
      config.i18n.load_path += Dir[Rails.root.join('config', 'locales', 
'**', '*.{rb,yml}')]

    config.exceptions_app = self.routes

    config.active_job.queue_adapter = :sidekiq
    config.after_initialize do
      ::Git_branch = `git rev-parse --abbrev-ref HEAD`
    end
  end
end

编辑 2:添加(希望是详尽的)Rails 在 development.log 中记录的堆栈跟踪:

ActionView::Template::Error (can't add a new key into hash during iteration):
     6:     <meta charset="utf-8" />
     7:     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     8:     <%= stylesheet_link_tag    'application', media: 'all' %>
     9:     <%= javascript_include_tag 'application' %>
    10:     <%= favicon_link_tag 'soundnotation8.ico' %>
    11:     <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
    12:

app/views/layouts/application.html.erb:9:in `_app_views_layouts_application_html_erb__320037179907821997_69815613536340'
app/controllers/errors_controller.rb:4:in `not_found'
  Rendering /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
  Rendering /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (8.5ms)
  Rendering /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
  Rendering /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
  Rendered /home/soundnotation/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (71.6ms)

你知道如何解决这个问题吗?提前非常感谢

【问题讨论】:

  • 错误页面无需添加 authentication_user。
  • 好的,但是如果我没有登录怎么办?页面会显示吗?如果我删除该行并调用/404 页面,它会将我重定向到登录页面。但这也是引发错误时的行为吗?
  • 这可能是第三方 gem 中的错误。您可以尝试此线程底部建议的故障排除技术吗? github.com/rails/rails/issues/24627#issuecomment-217475139
  • 对不起@MattBrictson :) 但我们最终使用了exception_handler。不管怎样,谢谢你的文章!

标签: ruby-on-rails devise runtime-error ruby-on-rails-5 custom-error-pages


【解决方案1】:

对不起@Matt Brictson :) 但我们最终还是使用了exception_handler

【讨论】:

    猜你喜欢
    • 2011-07-16
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 2014-12-17
    相关资源
    最近更新 更多