Ruby-on-rails 如何重定向到routes.rb 中的404 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19653761/
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
How redirect to 404 page in routes.rb?
提问by piton4eg
How can I redirect incorrect url to 404 page in routes.rb? Now I use 2 examples code:
如何将错误的 url 重定向到 routes.rb 中的 404 页面?现在我使用 2 个示例代码:
# example 1
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(params[:url]).to_s }, as: :redirect, format: false
# example 2
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(URI.encode(params[:url])).to_s }, as: :redirect, format: false
But when I try using russian words in 'url' parameter, in 1st example I get 500 page (bad URI), in 2nd - I get redirect to stage.example.xn--org-yedaaa1fbbb/
但是,当我尝试在 'url' 参数中使用俄语单词时,在第一个示例中,我得到 500 页(错误的 URI),在第二个示例中 - 我重定向到 stage.example.xn--org-yedaaa1fbbb/
Thanks
谢谢
回答by Richard Peck
If you want custom error pages, you'll be best looking at this answerI wrote a few weeks ago
如果你想要自定义错误页面,你最好看看我几周前写的这个答案
You need several important elements to create custom error routes:
您需要几个重要元素来创建自定义错误路由:
-> Add custom error handler in application.rb:
->添加自定义错误处理程序application.rb:
# File: config/application.rb
config.exceptions_app = self.routes
-> Create /404routes in your routes.rb:
- >创建/404您的路线routes.rb:
# File: config/routes.rb
if Rails.env.production?
get '404', :to => 'application#page_not_found'
end
-> Add actionsto application controller to handle these routes
->添加actions到应用程序控制器来处理这些路由
# File: app/controllers/application_controller.rb
def page_not_found
respond_to do |format|
format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
This is obviously relatively basic, but hopefully it will give you some more ideas on what you can do
这显然是相对基本的,但希望它会给你更多关于你能做什么的想法
回答by cassanego
The simplest thing to do is to make sure your routes do not match bad URLs. By default Rails will return a 404 for routes that do not exist.
最简单的方法是确保您的路由不匹配错误的 URL。默认情况下,Rails 会为不存在的路由返回 404。
If you can't do this the default 404 page lives at /404so you can redirect to that location. Something to keep in mind here, however, is that this type of redirect will perform a 301 Permanent Redirect instead of a 302. This may not be the behavior that you want. To do this you can do the following:
如果您不能这样做,则默认 404 页面位于,/404以便您可以重定向到该位置。但是,这里要记住的是,这种类型的重定向将执行 301 永久重定向而不是 302。这可能不是您想要的行为。为此,您可以执行以下操作:
match "/go/(*url)", to: redirect('/404')
Instead, I'd recommend setting up a before filter in your action that instead raises a not found exception. I'm not exactly sure if this exception is in the same place in Rails 4, but with Rails 3.2 I'm currently using:
相反,我建议在您的操作中设置一个 before 过滤器,而不是引发未找到的异常。我不确定这个异常是否在 Rails 4 中的同一个地方,但我目前正在使用 Rails 3.2:
raise ActionController::RoutingError.new('Not Found')
You can then do any processing and URL checking in the controller (if complicated checking of the URL format is required).
然后您可以在控制器中进行任何处理和 URL 检查(如果需要对 URL 格式进行复杂的检查)。

