在Rails中进行路由。 URL中的点 [英] Routing in Rails. Dots in URL

查看:68
本文介绍了在Rails中进行路由。 URL中的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类别模型中覆盖了 to_param 方法

def to_param
  name
end

routes.rb

get '/:id' => 'categories#show', :as => :category

name 参数没有包含任何点( foobar ),一切正常,但是当它包含( foobar
时,我得到错误没有路由匹配[GET] 。所以我的问题是:是否可以像参数名称的一部分那样在路由中使用点?或者我可以做些什么来达到这个目的,也许有些钩子或其他东西。感谢您的帮助。

When name parameter doesn't contain any dots (foobar), all works right, but when it does (f.o.o.b.a.r) I get an error No route matches [GET]. So my question is: is it possible to use dots in routing like a part of name of parameter? Or what can I do to accomplish this purpose, maybe some hooks or something. Any help is appreciated.

推荐答案

您可以更改此路线的约束:

You can change the constraints for this route:

get ':/id' => "categories#show", :as => :category, :constraints => { :id => /[\w+\.]+/ }

此路线现在将匹配:id 到包含任何单词字符或点的任何字符串。

This route will now match :id to any string containing any word character or a dot.

这篇关于在Rails中进行路由。 URL中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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