2014-01-19 36 views
0

我正在使用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 

有另一种方式来创造比在路由文件的第二个例子以外的事情控制的自定义路线?

虽然这会创建正确的路由,但会破坏当前正在工作的大部分代码。

在此先感谢您提供帮助我解决此问题的任何帮助,我们将不胜感激。

回答

0

好吧,你在声明你的路线时犯的一个非常微妙的错误是你没有提供对'事物'资源的阻止。因此,正如您在输出中看到的那样,Rails将为您的路由中的相同操作创建两条重复路径,即kits_like_path映射到kits#like由于get :like, on: :member仅适用于'工具包'资源。 所以这只是为了工作,提供块,而宣称“东西”资源如下:

resources :kits do 
    get :like, :on => :member 
    resources :things, shallow: true do 
     get :like, :on => :member 
    end 
    end 

耙路线命令的输出将是如下:

 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 

我希望这会帮助你。