我正在使用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
运行佣金路线:
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)thing#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
然后正确地为事物控制器创建路线。
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
除路径文件的第二个示例外,还有另一种方法可以创建到事物控制器的自定义路由吗?
虽然这会创建正确的路由,但它会破坏当前正在运行的大部分代码。
提前感谢您提供的帮助我解决此问题的任何帮助,我们将不胜感激。
答案 0 :(得分:0)
嗯,你在申报路线方面所犯的一个非常微妙的错误就是你没有提供阻止这些事情的事情'资源。因此,正如您在输出中看到的,Rails正在为您的路线中的相同操作创建两个重复路径, kits_like_path
映射到kits#like
因为get :like, on: :member
已应用于&#39 ;试剂盒'仅限资源。
因此,只是为了使其工作提供阻止,同时宣布'事物'资源如下:
resources :kits do
get :like, :on => :member
resources :things, shallow: true do
get :like, :on => :member
end
end
rake routes命令的输出如下:
like_kit GET /kits/:id/like(.:format) kits#like
like_thing GET /things/:id/like(.:format) things#like
kit_things GET /kits/:kit_id/things(.:format) things#index
POST /kits/:kit_id/things(.:format) things#create
new_kit_thing GET /kits/:kit_id/things/new(.:format) things#new
edit_thing GET /things/:id/edit(.:format) things#edit
thing GET /things/:id(.:format) things#show
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
kits GET /kits(.:format) kits#index
POST /kits(.:format) kits#create
new_kit GET /kits/new(.:format) kits#new
edit_kit GET /kits/:id/edit(.:format) kits#edit
kit GET /kits/:id(.:format) kits#show
PUT /kits/:id(.:format) kits#update
DELETE /kits/:id(.:format) kits#destroy
我希望这会对你有所帮助。