49

I want to test the 500 error pages in my Rails app using the development environment.

I already tried this in config/environments/development.rb:

config.action_controller.consider_all_requests_local = false

But this does not seem to have any effect.

pnuts
  • 58,317
  • 11
  • 87
  • 139
tbk
  • 1,516
  • 3
  • 14
  • 21
  • 1
    Would changing your environment to Production help? – Nuby Mar 10 '11 at 18:00
  • I personally think that it's better to switch to the production env for testing error pages. If you always disable Rails's built-in error pages in development, you could be making it more difficult to see valuable debugging information. – Nick McCurdy Nov 20 '13 at 02:43

8 Answers8

85

The only way I've found to do this so far is to set in development.rb

config.consider_all_requests_local = false

Now, restart the server.

Then access the URLs using my local IP address: http://192.168.1.135:3000/blah

The other settings mentioned don't seem to have any effect.

notapatch
  • 6,569
  • 6
  • 41
  • 45
u2622
  • 2,991
  • 3
  • 25
  • 27
  • 4
    Also works in Rails 4. BTW, it works for me also using either 127.0.0.1:3000/page_which_does_not_exist or localhost:3000/page_which_does_not_exist – Ben Aug 30 '13 at 17:22
  • Does not work for me. I'm getting no route matches instead of internal server error. – oky_sabeni Mar 06 '14 at 20:49
  • 6
    After setting config.consider_all_requests_local = false don't forget to restart the server. – Ross Sep 25 '14 at 09:42
30

None of the proposed solutions worked in my Rails 3 app. The quick and dirty solution for me was to simply hit the error pages directly to see the rendered HTML. For example,

http://0.0.0.0:3000/404.html

http://0.0.0.0:3000/500.html
Jeff Poulton
  • 6,032
  • 1
  • 21
  • 12
  • It does not work for me. I'm getting the 500 error page but I'm seeing it being logged by the rails server or the development log. – oky_sabeni Mar 06 '14 at 20:53
24

Just do a http://localhost:3000/404 or /500 to access these pages and see how they look like.

Stephane Paquet
  • 2,315
  • 27
  • 31
13

You can either:

  1. access the application using address other than localhost or 127.0.0.1 which rails considers by default to be local requests
  2. Override local_request? in application_controller.rb to something like:
def local_request?
  false
end

The second will stop rails treating requests from localhost and 127.0.0.1 as local requests which combined with consider_all_requests_local = false should show you your 500.html page.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
9

In addition to setting: config.consider_all_requests_local = false I also needed to set: config.action_dispatch.show_exceptions = true

Aaron Henderson
  • 1,840
  • 21
  • 20
  • Note that this should be set before server start and has no effect afterwards (= may not be suitable for auto testing where you need both behaviours) – Vasfed Dec 16 '19 at 19:16
4

You should add the below lines to the application_controller,

unless  ActionController::Base.consider_all_requests_local
    rescue_from Exception, :with => :render_500
    if  ActiveRecord::RecordNotFound
      rescue_from Exception, :with => :render_404
    end
    rescue_from ActionController::RoutingError, :with => :render_404
    rescue_from ActionController::UnknownController, :with => :render_404
    rescue_from ActionController::UnknownAction, :with => :render_404
end

Then try running with the below settings.

config.action_controller.consider_all_requests_local = false in config/environments/development.rb:

It will work. Please dont forget to write the function in application_controller.rb to render the layout for each of the error messages.

Unni
  • 165
  • 3
  • 10
  • You could also just use the `rescue_from` directive at the top of the controller, as in http://guides.rubyonrails.org/action_controller_overview.html#the-default-500-and-404-templates – dooleyo Aug 21 '13 at 22:24
  • ActionController::UnknownAction doesn't exist anymore – webdevguy Mar 16 '16 at 21:37
0

I think the proper setting to twiddle is this:

config.action_view.debug_rjs = false

Why it's still labelled rjs isn't entirely clear.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

if you just want to force a 500 error to see what it looks like, you can just add this to a view:

haml example:

= render :partial => "broken", :status => 500