Currently in production im getting this text:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's
log file and/or the web server's log file to find out what went wrong.
No html in that page nothing.
Where is this code situated? I have no public/500.html or anything in that regard.
In my routes I have:
get "/404", :to => "errors#error_404"
get "/422", :to => "errors#error_404"
get "/500", :to => "errors#error_500"
get "/505", :to => "errors#error_505"
ErrorsController:
class ErrorsController < ApplicationController
def sub_layout
"left"
end
def error_404
render :status => 404, :formats => [:html], :layout => "white", :sub_layout => "left"
end
def error_422
render :status => 422, :formats => [:html], :layout => "white", :sub_layout => "left"
end
def error_500
render :status => 500, :formats => [:html], :layout => "white", :sub_layout => "left"
end
def error_505
render :status => 505, :formats => [:html], :layout => "white", :sub_layout => "left"
end
end
How to make it load my custom errors always? On some errors it just throw that 2 line text coming somewhere from rails core, I want it to pickup my custom styled error pages every time! how? thx!
The Error you are experiencing is being thrown from
https://github.com/rails/rails/blob/4-0-stable/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L18-L22
This means, the code your exceptions are getting rescued by are themselves throwing exceptions. You can check your logs for text:
Error during failsafe response:
to identify what the exceptions really are originating from and thus solve your problem.
Error pages in application should be as simple as it possible. Same recommendation concerns their rendering. If your application returns 500 HTTP response code it means that things got wrong already. And there is a chance you could not render error page and display it to user.
Ideally error pages should be a plain HTML served directly by your web server without hitting application server.
Speaking of Rails implementation of this idea. It based on using asset pipeline for precompiling the HTML static pages.
Firstly add new assets type (Rails > 4.1):
If templating engine is using (e.g. slim, haml), register it via initializer:
Now you ready to create pretty error pages in app/assets/html directory using your favorite template engine and Rails built-in view helpers.
Tips for production
On production asset pipeline adds digest to compiled assets and stores files under default folder (typically shared/public/assets on production server). You can use capistrano to copy error pages to web server root:
And last thing. Tell web server to use these files for certain HTTP error codes (sample nginx configuration):
Sprocket 3 Update
For Sprocket 3 you need something like this (tested with Rails 5):
Here is latest and quick fix for showing custom 404_error page.