简体   繁体   English

处理Rails 3.1应用程序中不存在的路由

[英]Handling non-existent routes in Rails 3.1 application

I just switched to rails not long ago and I'm loving it. 我不久前刚转到轨道,我很喜欢它。 Everything works well in my rails 3.1 application, but now at the end I want to somehow handle routes like www.myapp.com/something (off course, I dont have something controller). 一切都在我的rails 3.1应用程序中运行良好,但现在最后我想以某种方式处理像www.myapp.com/something这样的路线(当然,我没有控制器)。 I get a routing error when I visit this page, but I was wandering if there was a way to do something about it, even if it's only redirecting these routes to my root_url. 当我访问这个页面时出现路由错误,但是如果有办法对它做一些事情我会徘徊,即使它只是将这些路由重定向到我的root_url。 I tried to find the answer online with no luck. 我试着在网上找到答案而没有运气。

Yes, you can put a globbing route at the very end of your routes.rb to catch all misses: 是的,您可以在routes.rb最后放置一个globbing路由来捕获所有未命中:

match '/*paths', :to => 'some_controller#some_action'

in your Controller / action you can access the globbed path with 在您的Controller / action中,您可以访问globbed路径

params[:paths]

more information http://guides.rubyonrails.org/routing.html#route-globbing 更多信息http://guides.rubyonrails.org/routing.html#route-globbing

of course you can redirect without using an extra controller by using the redirect inline rack endpoint 当然,您可以使用redirect内联机架端点重定向而无需使用额外的控制器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM