要映射到rails 4中的自定义路径的资源路由

whi*_*eed 11 custom-routes ruby-on-rails-4

我有一条路线:

resources :products
Run Code Online (Sandbox Code Playgroud)

现在我已经将所有代码都安装到位,但只需要更改路径 /products/:action to /items/:action

我已经浏览了rails docs但是无法解决这个问题.它看起来很基本,应该很容易,但我不能把手指放在上面.

我使用的网址是:http://guides.rubyonrails.org/routing.html#path-and-url-helpers

dgi*_*rez 15

你可以这样写你的路线:

resources :products, path: 'items'
Run Code Online (Sandbox Code Playgroud)

这将/items使用product_*命名助手生成路由ProductsController.看一下路由指南的这一部分.


Bar*_*cha 10

有几种方法可以实现这一目标.一种是简单地命名您的资源items并使用该:controller选项指定控制器.

resources :items, controller: 'products'
Run Code Online (Sandbox Code Playgroud)

这将识别以路径开头/items但路由到的路径ProductsController.它还将根据资源名称生成路径助手(例如items_pathnew_item_path).

另一种方法是:path在指定@dgiperez指出的资源时使用该选项.

resources :products, path: 'items'
Run Code Online (Sandbox Code Playgroud)

这也将路径开始路由/itemsProductsController但由于路由助手基于资源名称,它们将基于产品(例如products_pathnew_product_path)

参考