Math is Hard
Math is Hard

Reputation: 942

Rails route change controller for index

I have a model Comment that belongs to both Image and Video. My routes.rb has

resources :images do
    resources :comments
end

resources :videos do
    resources :comments
end

Both endpoints hits comments_controller#index which is a problem. Is there a way I can get them to hit different functions for the controller such as comments_controller#image_index and comments_controller#video_index. I prefer not do to do the hacky

get 'images/:id/comments' => 'controller#image_index'

Thanks for reading

Upvotes: 0

Views: 554

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42909

Can't think of a better way yet, but this would work

resources :images do
  resources :comments, except: :index do
    collection do
      get '/', to: :images_index
    end
  end
end

resources :videos do
    resources :comments
end

Upvotes: 2

Related Questions