Alan Coromano
Alan Coromano

Reputation: 26048

Nested resources and on: collection don't do what I expect from them

My routes.rb

   resources :users do
      resources :tags, on: :collection
      get :some_page, on: :collection
    end

and rake routes

users/:user_id/tags(.:format) --- tags#index
users/:user_id/tags(.:format) --- tags#create
users/:user_id/tags/new(.:format) --- tags#new
//......
users/some_page(.:format) --- users#some_page

and there are 2 controllers: UsersController and UsersTagsController.

I want the routes for tags to be:

users/tags(.:format) --- users_tags#index
users/tags(.:format) --- users_tags#create
users/tags/new(.:format) --- users_tags#new
//............

Why don't they look like this and how do I fix this?

Upvotes: 0

Views: 40

Answers (2)

siekfried
siekfried

Reputation: 2964

I guess it's because :on only work with single routes. Try this instead:

resources :users do
  collection do
    resources :tags
  end
end

Upvotes: 1

Marek Lipka
Marek Lipka

Reputation: 51191

You should name your controller Users::TagsController And then in routes:

namespace :users do
  resources :tags
end

Upvotes: 0

Related Questions