我得到了以下错误:
ActionController::RoutingError (No route matches [GET] "/images/favicon.ico")
我想要显示不存在的链接的error404页面。
我怎样才能做到这一点呢?
发布于 2014-09-15 14:34:57
在application_controller.rb
中添加以下内容:
# You want to get exceptions in development, but not in production.
unless Rails.application.config.consider_all_requests_local
rescue_from ActionController::RoutingError, with: -> { render_404 }
end
def render_404
respond_to do |format|
format.html { render template: 'errors/not_found', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
我通常也会解救以下异常,但这取决于你:
rescue_from ActionController::UnknownController, with: -> { render_404 }
rescue_from ActiveRecord::RecordNotFound, with: -> { render_404 }
创建errors控制器:
class ErrorsController < ApplicationController
def error_404
render 'errors/not_found'
end
end
然后在routes.rb
中
unless Rails.application.config.consider_all_requests_local
# having created corresponding controller and action
get '*path', to: 'errors#error_404', via: :all
end
最后一件事是在/views/errors/
下创建not_found.html.haml
(或您使用的任何模板引擎
%span 404
%br
Page Not Found
https://stackoverflow.com/questions/25841377
复制相似问题