我正在使用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路线正确创建,我能够像从套件控制器套件。
我想解决的问题是能够创建一个将路由到东西控制器的like_things_path。
like_things_path GET /things/:id/like(.:format)的东西#像
如果我改变路线文件,以这样的:
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
然后正确地创建路线的事物控制器。
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
有另一种方式来创造比在路由文件的第二个例子以外的事情控制的自定义路线?
虽然这会创建正确的路由,但会破坏当前正在工作的大部分代码。
在此先感谢您提供帮助我解决此问题的任何帮助,我们将不胜感激。