Rails custom 404 page

Add your project 404 pages for no route errors and ActiveRecord::RecordNotFound errors


app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  #...

  rescue_from ActiveRecord::RecordNotFound, :with => :render_404

  def render_404
    render :template => "errors/error", :status => 404
  end
  #...
end
app/views/errors/error.html
<div class="jumbotron">
  <h1>404</h1>
  <br />
  <p>This page is no longer here but you can create one!</p>
</div>
app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
end
config/routes.rb
# this should be the last line of routes.rb
match '*rest', to: 'errors#error', via: :all
config/application.rb
module YourApp
  class Application < Rails::Application
    #...  
    config.exceptions_app = self.routes
    #...
  end
end

Share This Post