0

I am using the act_as_votable gem to be able to like objects from two different models.

Everything works as expected in the Rails Console.

I am having trouble being able to create the route that I need in controller whose resources are being nested.

The route file currently looks like this.

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

Running rake routes:

    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

The route to the like_kit_path is created correctly and I am able to like a kit from the Kits controller.

The problem I am trying to solve is being able to create a like_things_path that will route to the things controller.

like_things_path GET /things/:id/like(.:format) things#like

If I change the route file to this:

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

Then the route is created correctly to the things controller.

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

Is there another way to create a custom route to the things controller other than in the second example of the route file?

While this creates the correct route, it breaks much of the code that is currently working.

Thanks in advance for any assistance you can provide in helping me solve this issue will be greatly appreciated.

1 Answers1

0

Well, the very subtle mistake that you have done in declaring your routes is you have not provided block to 'things' resource. Hence As you can see in output, Rails is creating two duplicate paths for the same action in your routes, which is kits_like_path mapped to kits#like As both get :like, on: :member have got applied on 'kits' resource only. So just to make it work provide block while declaring 'Things' resource as follow:

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

The output of rake routes command would be as follow:

        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

I hope this will help you.

chiwangc
  • 3,566
  • 16
  • 26
  • 32
user2015646
  • 39
  • 1
  • 4