Rails:将所有未知路由重定向到 root_url [英] Rails: redirect all unknown routes to root_url

查看:21
本文介绍了Rails:将所有未知路由重定向到 root_url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户访问错误页面时,rails 都会显示公共文件夹中的 404.html.但是,我只想将浏览器重定向到根页面,而不显示任何内容.所以我尝试了 globbing,但无济于事,它仍然显示 404 页面.这是我的路由文件的摘录:

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:

# ...
map.root :controller => 'home', :action => 'home'
map.connect '*', :controller => 'home', :action => 'home'

有什么建议吗?谢谢各位!

Any suggestions? Thanks, guys!

推荐答案

如果您的项目由 rails 3 提供支持,只需将此行添加到您的 routes.rb

If your project is powered by rails 3, add simply this line to your routes.rb

match '*path' => redirect('/')

如果您使用的是 Rails 4 或 5

If you're on Rails 4 or 5

match '*path' => redirect('/'), via: :get

get '*path' => redirect('/')

这篇关于Rails:将所有未知路由重定向到 root_url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆