How to rescue page not found 404 in rails? How to rescue page not found 404 in rails? ruby-on-rails ruby-on-rails

How to rescue page not found 404 in rails?


Solution for Rails 4

On routes.rb:

get '*unmatched_route', to: 'application#not_found'

On application_controller.rb:

def not_found  # Your exception handling code hereend


i found the solution, check this out => http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution (great solution)

routes.rb :

# at the end of you routes.rbmatch '*a', :to => 'errors#routing', via: :get

errors_controller.rb :

class ErrorsController < ApplicationController  def routing    render_404  endend

application.rb :

rescue_from ActionController::RoutingError, :with => :render_404 private  def render_404(exception = nil)    if exception        logger.info "Rendering 404: #{exception.message}"    end    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false  end