Recommended books

Custom 404 Rails error pages

Ruby posted about 1 year ago by christian

All credit for this snippet is due to Henrik Nyh.

Put this in application_controller.rb:

   1  alias_method :rescue_action_locally, :rescue_action_in_public if RAILS_ENV == 'development'
   2    
   3    def render_optional_error_file(status_code)
   4      if status_code == :not_found
   5        activate_authlogic
   6        render_404
   7      else
   8        super
   9      end
  10    end
  11  
  12    def render_404
  13      respond_to do |type| 
  14        type.html { render :template => "errors/error_404", :layout => 'application', :status => 404 } 
  15        type.all  { render :nothing => true, :status => 404 } 
  16      end
  17  
  18      true  # so we can do "render_404 and return"
  19    end

Tagged 404, rails, custom, error