【发布时间】:2014-01-19 19:21:14
【问题描述】:
我正在使用 act_as_votable gem 来喜欢来自两个不同模型的对象。
在 Rails 控制台中一切正常。
我无法在其资源被嵌套的控制器中创建我需要的路由。
路由文件目前看起来像这样。
KitsIo::Application.routes.draw do
resources :kits do
get :like, :on => :member
resources :things, shallow: true
get :like, :on => :member
end
root 'kits#index'
end
运行 rake 路线:
like_kit_path GET /kits/:id/like(.:format) kits#like
kit_things_path GET /kits/:kit_id/things(.:format) things#index
POST /kits/:kit_id/things(.:format) things#create
new_kit_thing_path GET /kits/:kit_id/things/new(.:format) things#new
edit_thing_path GET /things/:id/edit(.:format) things#edit
thing_path GET /things/:id(.:format) things#show
PATCH /things/:id(.:format) things#update
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
GET /kits/:id/like(.:format) kits#like
kits_path GET /kits(.:format) kits#index
POST /kits(.:format) kits#create
new_kit_path GET /kits/new(.:format) kits#new
edit_kit_path GET /kits/:id/edit(.:format) kits#edit
kit_path GET /kits/:id(.:format) kits#show
PATCH /kits/:id(.:format) kits#update
PUT /kits/:id(.:format) kits#update
DELETE /kits/:id(.:format) kits#destroy
root_path GET / kits#index
到 like_kit_path 的路由已正确创建,我可以从 Kits 控制器中喜欢一个工具包。
我要解决的问题是能够创建一个 like_things_path 来路由到 things 控制器。
like_things_path GET /things/:id/like(.:format) things#like
如果我把路由文件改成这样:
KitsIo::Application.routes.draw do
resources :kits do
get :like, :on => :member
resources :things do
get :like, :on => :member
end
end
root 'kits#index'
end
然后正确地创建到 things 控制器的路由。
like_kit_path GET /kits/:id/like(.:format) kits#like
like_kit_thing_path GET /kits/:kit_id/things/:id/like(.:format) things#like
kit_things_path GET /kits/:kit_id/things(.:format) things#index
POST /kits/:kit_id/things(.:format) things#create
new_kit_thing_path GET /kits/:kit_id/things/new(.:format) things#new
edit_kit_thing_path GET /kits/:kit_id/things/:id/edit(.:format) things#edit
kit_thing_path GET /kits/:kit_id/things/:id(.:format) things#show
PATCH /kits/:kit_id/things/:id(.:format) things#update
PUT /kits/:kit_id/things/:id(.:format) things#update
DELETE /kits/:kit_id/things/:id(.:format) things#destroy
kits_path GET /kits(.:format) kits#index
POST /kits(.:format) kits#create
new_kit_path GET /kits/new(.:format) kits#new
edit_kit_path GET /kits/:id/edit(.:format) kits#edit
kit_path GET /kits/:id(.:format) kits#show
PATCH /kits/:id(.:format) kits#update
PUT /kits/:id(.:format) kits#update
DELETE /kits/:id(.:format) kits#destroy
root_path GET / kits#index
除了路由文件的第二个示例之外,还有其他方法可以创建到 things 控制器的自定义路由吗?
虽然这会创建正确的路线,但它会破坏当前正在运行的大部分代码。
在此先感谢您在帮助我解决此问题时提供的任何帮助,我们将不胜感激。
【问题讨论】:
标签: controller routing member custom-routes