10

I think Rails 3.1 is changing the way that errors are raised. Can anyone assist or confirm this? I'm attempting to create custom errors pages with Rails 3.1.0.rc1

unless config.consider_all_requests_local
    rescue_from Exception, :with => :render_error
    rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
    rescue_from ActionController::RoutingError, :with => :render_not_found
    rescue_from ActionController::UnknownController, :with => :render_not_found
    rescue_from ActionController::UnknownAction, :with => :render_not_found
end

^^ This doesn't work.

config.consider_all_requests_local       = true

That is in my development environment by default. I'm assuming Rails 3.1 removes the "action_controller" but I can't confirm this anywhere.

Thanks!

Robert Ross
  • 1,895
  • 2
  • 23
  • 32

2 Answers2

19

I'm assuming the following code appears in your ApplicationController?

unless config.consider_all_requests_local
  rescue_from Exception, :with => :render_error
  rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
  rescue_from ActionController::RoutingError, :with => :render_not_found
  rescue_from ActionController::UnknownController, :with => :render_not_found
  rescue_from ActionController::UnknownAction, :with => :render_not_found
end

If so, try replacing this line:

unless config.consider_all_requests_local

with this line (pre Rails 3 I think):

unless ActionController::Base.consider_all_requests_local

or this (post Rails 3):

unless Rails.application.config.consider_all_requests_local
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
9

I don't believe Matt's solution will catch routing errors in Rails 3.0/3.1.

Try putting the following in your application.rb:

# 404 catch all route
config.after_initialize do |app|
  app.routes.append{ match '*a', :to => 'application#render_not_found' } unless config.consider_all_requests_local
end

See: https://github.com/rails/rails/issues/671#issuecomment-1780159

Worked well for me!

dleavitt
  • 1,386
  • 13
  • 14
  • Thanks for adding this! Learned about it a few weeks after posting my original answer here. :) – Matt Huggins Nov 05 '11 at 00:20
  • in general things that end up being the equiv. of if !Rails.env.production are dangerous... – Kevin Nov 08 '11 at 00:38
  • @Kevin - Where does this happen? Is this in reference to the use of `consider_all_request_local`? If so, I don't think you make a valid argument, as this is a setting defined on a per-environment basis. There are numerous settings configured per-environment, and this is just one of many. – Matt Huggins Nov 29 '12 at 17:01
  • It's been awhile, but I agree and withdraw my complaint :) – Kevin Nov 30 '12 at 18:37