Rails 路线 - 如何使它们不区分大小写?
Ruby on Rails 中的路由区分大小写。好像之前有人提出过这个问题,而且已经标记为无法解决。
http://rails.lighthouseapp.com/projects/8994 /tickets/393-routes-are-case-sensitive
这让我感到不幸,因为我并没有真正看到我自己的应用程序对区分大小写的路线有任何好处,而缺点是它创造了潜在的风险在我看来,这是因为混乱和缺乏修饰的总体外观。
使我的路线不区分大小写的最佳方法是什么?
我在 Google 搜索中发现了这个提示:
map.connect "web_feeds/:action", :controller => 'web_feeds', :action => /[a-z_]+/i
这很聪明,但它仍然使 url 的 web_feeds 部分区分大小写。然而,如果不手动输入 wEb_feEds 的每种可能的组合,我没有看到任何类似的解决方案,但这显然是一个可怕的解决方案,原因有很多。
Routes in Ruby on Rails are case sensitive. It seems someone brought this up before, and it has been labeled will not fix.
http://rails.lighthouseapp.com/projects/8994/tickets/393-routes-are-case-sensitive
That strikes me as unfortunate, as I don't really see any upside on my own application for routes to be case sensitive, while on the downside it creates a potential for confusion and a general appearance of lack of polish in my opinion.
What's the best way to make my routes case insensitive?
I found this tip on a Google search:
map.connect "web_feeds/:action", :controller => 'web_feeds', :action => /[a-z_]+/i
This is clever, but it still leaves the web_feeds portion of the url case sensitive. I don't see any similar way around this, however, without entering in each possible combination of wEb_feEds manually, but that is obviously a horrible solution for any number of reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Rails 中的路由区分大小写,因为 URL 区分大小写。来自W3C:
Routes in Rails are case sensitive because URLs are case sensitive. From the W3C:
我刚刚遇到了同样的问题,并使用中间件解决了它 - 看看这里:
http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/
注意:这仅适用于 Rails 2.3+
I've just has the same problem, and solved it using middleware - have a look here:
http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/
Note: This only applies for Rails 2.3+
好吧,你可以尝试另一种方法。使 case 转换服务器端并将所有内容发送到 Rails Downcase。
我认为你可以通过 mod_rewrite 或 mod_spelling 来实现这一点。
Well you could try another approach. Make the case transform serverside and send everything to rails downcase.
I think you can achieve this with either mod_rewrite or mod_spelling.
一个简单的解决方案是,可能不是一种优雅的方式,但可行的是:
像这样在应用程序控制器中使用 before_filter 。
正如我已经说过的,它不是一个优雅但可行的方法,所以请不要投反对票。 :P
A simple solution is, may be not an elegant way, but yet workable is:
Use a before_filter in your application controller like this.
As i already told that its not an elegant but yet workable, so no down votes please. :P
只需将其修补为默认小写即可。简单的例子:
just monkey-patch it to downcase by default. simple example:
尽管 URL 区分大小写,但如果您想让路由不区分大小写,则可以使用一种肮脏的 hack 方法。
在 application_controller.rb 中输入:
但实际上不要这样做。您为任何不存在的路由创建重定向循环。您确实应该将 request.request_uri 解析为其组件,将它们小写,并使用它们生成您重定向到的合法路由。正如我刚才提到的,这是一个肮脏的黑客行为。然而,我认为这比让你的路线图变得丑陋和黑客要好。
Though URLs are case-sensitive, if you want to make your routes case-insensitive, there is a dirty hack you can do.
In application_controller.rb put:
But don't actually do that. You create a redirect loop for any non-existant routes. You really should parse request.request_uri into its components, downcase them, and use them to generate the legit route that you redirect to. As I mentioned right off, this is a dirty hack. However, I think this is better than making your route map ugly and hackish.
(几年后发布)
如果您只寻找不区分大小写的特定路线,就像我的情况一样,我最终做的是以下内容。
假设您的路线是
这将解析
/item/123
但不会解析/ITEM/123
或Item/123
。您可以使用分段约束,将
item
视为参数,并添加默认值,这样您的路线助手就不需要指定它(因此保持上面原始路线的样子):你在
/item
下有多个路由,你可以用一个范围来概括这个概念:(Posting this years later)
If you are looking only for specific routes to be case insensitive, like it was in my case, what I ended up doing is the following.
Say your route is
This will resolve
/item/123
but not/ITEM/123
norItem/123
.You can use a segment constraint, treat
item
as a param, and add a default so your routes helpers don't need to specify it (hence remain what they would be with the original route above):If you have multiple routes under
/item
, you can generalise this concept with a scope: