Ruby-on-rails Rails:将所有未知路由重定向到 root_url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132039/
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
Rails: redirect all unknown routes to root_url
提问by Albus Dumbledore
Whenever a user hits the wrong page, rails shows 404.html from the public folder. However, I'd like just to redirect the browser to the root page, without showing anything. So I tried globbing, but came to no avail, it still shows the 404 page. Here's an extract from my routes file:
每当用户访问错误页面时,rails 都会显示公共文件夹中的 404.html。但是,我只想将浏览器重定向到根页面,而不显示任何内容。所以我尝试了 globbing,但无济于事,它仍然显示 404 页面。这是我的路由文件的摘录:
# ...
map.root :controller => 'home', :action => 'home'
map.connect '*', :controller => 'home', :action => 'home'
Any suggestions? Thanks, guys!
有什么建议?谢谢你们!
回答by Arkan
If your project is powered by rails 3, add simply this line to your routes.rb
如果您的项目由 rails 3 提供支持,只需将此行添加到您的 routes.rb
match '*path' => redirect('/')
Edit: If you're on Rails 4 or 5
编辑:如果您使用 Rails 4 或 5
match '*path' => redirect('/'), via: :get
or
或者
get '*path' => redirect('/')
回答by Sam Wilder
Like the answer by Arkan. One point, if do not want this behaviour in development environment, then could do -
就像 Arkan 的回答一样。有一点,如果不希望在开发环境中出现这种行为,那么可以这样做 -
match '*path' => redirect('/') unless Rails.env.development?
回答by user
Rails 4-
导轨 4-
(routes.rb)
(路线.rb)
You can still use a simple getto redirect all unknown routes.
您仍然可以使用简单get的重定向所有未知路由。
get '*path', to: 'home#index'
If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.
如果您希望为 POST 和 GET 请求提供路由,您仍然可以使用 match,但 Rails 希望您通过via.
match "*path" => "home#index", via: [:get, :post]
Remember that routes.rbis executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.
请记住,这routes.rb是按顺序执行的(匹配适合提供的路径结构的第一条路线),因此将通配符捕获放在匹配项的底部。
回答by tomb
There seems to be a bug in rails 5.2 where active_storage routes are picked up by the catchall route, resulting in broken links to uploaded images. The issue has been reported in the rails repo on github, and someone commented with the below patch until the bug gets fixed in a new release:
rails 5.2 中似乎存在一个错误,其中 active_storage 路由被 catchall 路由获取,导致上传图像的链接断开。该问题已在 github 上的 rails repo 中报告,并且有人评论了以下补丁,直到该错误在新版本中得到修复:
In routes.rbright before last end
在routes.rb去年前右end
get '*all', to: 'application#index', constraints: lambda { |req|
req.path.exclude? 'rails/active_storage'
}
then in the application controller add:
然后在应用程序控制器中添加:
def index
flash.notice = 'No page found at that address'
redirect_to root_path
end
回答by shingara
You need create a controller to do that
你需要创建一个控制器来做到这一点
class RedirectsController
def index
redirect_to root_url
end
end
And in your routes
在你的路线中
map.connect '*', :controller => 'redirects', :action => 'index'

