【发布时间】:2018-04-16 12:25:56
【问题描述】:
假设,我在 routes.rb 中有这段代码
get "/items/(:brand)", to: "items#index", as: :items
我无法更改此路线,因为有时我需要路径中带有品牌的网址(而不是查询中)。 我可以创建这样的路径吗:
/items?brand=TestBrand
但不是这个:
/items/TestBrand
通过路由助手?
items_path(brand: "TestBrand") 给出第二个变体。
【问题讨论】:
-
get "/items/(:brand)", to: "items#index", as: :items是一条非常糟糕的路线,因为它违反了 RESTful 约定,并与正常的演出路线 (GET /items/:id) 产生了歧义。你为什么不能把它改成更好的东西,比如GET /brands/:brand_id/items? -
我需要用遗留代码支持大块 :(,实际上这个代码比我的例子复杂得多
-
那么您需要决定是否需要进行重大更改,因为现有代码实际上可能无法按预期工作。
标签: ruby-on-rails rails-routing