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

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

问题描述

每当用户点击错误的页面时,rails都会显示公用文件夹中的404.html。但是,我只想将浏览器重定向到根页面,而不显示任何内容。所以我尝试了通配符,但无济于事,它仍然显示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驱动的,只需将这行添加到您的route.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天全站免登陆