Ruby-on-rails 在 Rails 4 中,来自 ActionController::RoutingError 的救援
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25841377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rescue_from ActionController::RoutingError in Rails 4
提问by Anna
I've got the following error:
我有以下错误:
ActionController::RoutingError (No route matches [GET] "/images/favicon.ico")
I want to show error404 page for links that are not existing.
我想为不存在的链接显示 error404 页面。
How can I achieve that?
我怎样才能做到这一点?
回答by Andrey Deineko
In application_controller.rbadd the following:
在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
I usually also rescue following exceptions, but that's up to you:
我通常也会拯救以下异常,但这取决于你:
rescue_from ActionController::UnknownController, with: -> { render_404 }
rescue_from ActiveRecord::RecordNotFound, with: -> { render_404 }
Create the errors controller:
创建错误控制器:
class ErrorsController < ApplicationController
def error_404
render 'errors/not_found'
end
end
Then in routes.rb
然后在 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
And the last thing is to create not_found.html.haml(or whatever template engine you use) under /views/errors/:
最后一件事是在以下位置创建not_found.html.haml(或您使用的任何模板引擎)/views/errors/:
%span 404
%br
Page Not Found
回答by Misu
@Andrey Deineko, your solution seems to work only for the RoutingErrors raised manually inside a conrtoller. If I try it with the url my_app/not_existing_path, I still get the standard error message.
@Andrey Deineko,您的解决方案似乎仅适用于在控制器RoutingError内手动提出的s。如果我尝试使用 url my_app/not_existing_path,我仍然会收到标准错误消息。
I guess this is because the application doesn't even reach the controllers, since Rails raises the error before.
我想这是因为应用程序甚至没有到达控制器,因为 Rails 之前引发了错误。
The trickthat solved the problem for me was to add the following line at the endof the routes:
为我解决问题的技巧是在路线的末尾添加以下行:
Rails.application.routes.draw do
# existing paths
match '*path' => 'errors#error_404', via: :all
end
to catch all not predefined requests.
捕获所有未预定义的请求。
Then in the ErrorsController you can use respond_toto serve html, json... requests:
然后在 ErrorsController 中,您可以respond_to用来提供 html、json... 请求:
class ErrorsController < ApplicationController
def error_404
@requested_path = request.path
repond_to do |format|
format.html
format.json { render json: {routing_error: @requested_path} }
end
end
end
回答by Anuja
Copying favicon imagein app/assets/imagesworked for me.
复制图标图像在app/assets/images对我来说有效。

