Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

In new router, collection doesn't add name of the parent resource

#5214

Between RoR 3 beta 4 and RoR 3 RC was changed router to support shallow routes. It seems it affected 'collection' in a way that doesn't have anything to do with shallow routes.

Testingapp::Application.routes.draw do
  resources :books do
    collection do
      resources :genres
    end
  end
end

In RC rake routes includes lines like:

new_genre GET    /books/genres/new(.:format)      {:controller=>"genres", :action=>"new"}

But what I expected was this line as generated in beta 4:

new_books_genre GET    /books/genres/new(.:format)      {:controller=>"genres", :action=>"new"}

I.e. collection in new router doesn't add the parent resources' name to the name of resources in the collection, which is wrong (according to guides).

Reported by xHire · July 27th, 2010 @ 02:04 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Timo Mika Gläßer
    Timo Mika Gläßer

    I think what is actually wrong in your example is the nesting of

        collection do
          resources :genres
        end
    

    where is should just read

      resources :books do
         resources :genres
      end
    

    But I have a similiar problem with the methods not being generated. ;)

    August 4th, 2010 @ 11:01 AM

  2. xHire
    xHire

    It might seem so, but your suggestion generates URLs like /books/:book_id/genres/new(.:format) but that's definitely not what I wanted. ;c) Anyway the problem is not that it is not working (because it is) but that it doesn't generate correct names of the routes.

    August 6th, 2010 @ 07:54 AM

  3. Timo Mika Gläßer
    Timo Mika Gläßer

    Ok, sorry my misunderstanding. Did you consider using the option "namespace => :books"? See http://www.slideshare.net/mbleigh/upgrading-to-rails-3 at slide 25.

    August 6th, 2010 @ 11:59 AM

  4. xHire
    xHire

    Yes, to be able to continue in development of my application I was forced to use scope :as => :books. But that's not solution, that's just a hack, a temporary workaround which will be dropped as soon as this bug will be fixed. :c)

    If it could be done with simple nesting, why should I look for any complicated solutions (as namespace is)?

    August 6th, 2010 @ 12:10 PM

  5. Andrew White
    Andrew White
    • State changed from new to wontfix
    • Importance changed from to Low

    The resource scopes collection, member and new aren't designed to have resources calls inside them - there are no tests for this functionality so it is essentially undefined. The reason it sort of works is that the mapper will automatically wrap the resources call inside a nested block as a convenience. It's better to use the proper api:

      resources :books
    
      namespace :books, :module => nil do
        resources :genres
      end
    

    You can leave off the :module => nil if you'd prefer the resources controller to be books/genres.

    August 20th, 2010 @ 12:56 AM

  6. xHire
    xHire

    The resource scopes collection, member and new aren't designed to have resources calls inside them - there are no tests for this functionality so it is essentially undefined.

    If the only problem is that my use case wasn't intended to exist, but isn't unwanted, would be proper patch for this issue accepted?

    I just want to know if I should use my current workaround (with scope which currently more suits my needs than your suggestion) or if I could try to fix this in Rails.

    August 24th, 2010 @ 01:41 PM

  7. Andrew White
    Andrew White

    The concept of nested resources implies ownership by the parent therefore the use case isn't intended to exist, so I'd use whatever alternative method of generating your routes you prefer.

    August 24th, 2010 @ 01:51 PM

  8. xHire