开发者

Why do routes with a dot in a parameter fail to match?

I've 开发者_如何学编程got an route for my users like /iGEL/contributions, which works fine. But now a user registered with a name like 'A.and.B.', and now the route fails to match, since the name contains dots.

My route:

get "/:user/contributions" => 'users#contributions'

Any ideas?


See the blue info box here:

By default dynamic segments don’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment add a constraint which overrides this – for example :id => /[^\/]+/ allows anything except a slash.

That would for example be:

get "/:user/contributions" => 'users#contributions', :constraints => { :user => /[^\/]+/ }


If your variable segment is the last one, then using the [^\/] regex will also eat the format. In such a case rather use:

/([^\/]+?)(?=\.json|\.html|$|\/)/


Looks like the following link answers your question.

http://avdi.org/devblog/2010/06/18/rails-3-resource-routes-with-dots-or-how-to-make-a-ruby-developer-go-a-little-bit-insane/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜