Search code examples
ruby-on-railsdeviseruntime-errorruby-on-rails-5custom-error-pages

Rails dynamic error pages and Devise: RuntimeError


I read and tried to apply what Matt Brictson says in his awesome article about dynamic error pages on Rails. I use Devise to authenticate users so I have to put authenticate_user to the errors_controller.rb

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

But this fails giving me a RuntimeError in Errors#not_found when I try to call the /404 page:

can't add a new key into hash during Iteration

enter image description here

EDIT: I post the application.rb code too:

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

EDIT 2: added (hopefully exhaustive) stack trace logged by Rails in 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)

Do you know how to solve this problem? Thanks a lot in advance


Solution

  • Sorry @Matt Brictson :) but we worked around in the end using exception_handler