Rails 请教一个路由问题

huobazi · 2012年11月19日 · 最后由 huobazi 回复于 2012年11月19日 · 2351 次阅读

我希望能构建类似这样的 url localhost:3000/items/category/1/xtype/0/tag/aaa 并且上面的三个参数可以删减,且顺序无关 比如 localhost:3000/items/xtype/0/tag/aaa 比如 localhost:3000/items/tag/aaa 比如 localhost:3000/items/ 比如 localhost:3000/items/xtype/0/tag/aaa/category/123

也就是说类似使用 QueryString 来拼接 如 locahost:3000/items/?category=1&tag=aaa&category=1

这三个 QueryString 顺序无关,并且可为空,可不传递

我尝试使用如下路由设置:

match 'items/category/:category_id/xtype/:xtype/tag/:tag_name' => 'items#index'
resources :items

发现,删减参数后就

http://localhost:3000/items/xtype/0/area/222 报错:

Routing Error

No route matches [GET] "/items/type/0/tag/222"

Try running rake routes for more information on available routes.

请各位老少爷们指教,谢谢!

你陷入了 RESTful 的 URL design 的误区。

下面那种才是对的.. 不是说有 query string 就不 RESTful.

#1 楼 @Saito 不是说 QueryString 不 SEO 友好么?

try

resources :items do match 'items/(category/:category_id/(xtype/:xtype/(tag/:tag_name)))' => 'items#index' end

"不是说 QueryString 不 SEO 友好么?"

已不确定,现代的搜索引擎比此更加智能。

#3 楼 @qisine 多谢,多谢,我试试。

我刚才闷了半天,自己做了个丑陋的方法

https://gist.github.com/4110779

需要 登录 后方可回复, 如果你还没有账号请 注册新账号