This project is archived and is in readonly mode.

#5214 ✓wontfix
xHire

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

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

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).

Comments and changes to this ticket

  • Timo Mika Gläßer

    Timo Mika Gläßer August 4th, 2010 @ 11:01 AM

    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. ;)

  • xHire

    xHire August 6th, 2010 @ 07:54 AM

    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.

  • Timo Mika Gläßer

    Timo Mika Gläßer August 6th, 2010 @ 11:59 AM

    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.

  • xHire

    xHire August 6th, 2010 @ 12:10 PM

    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)?

  • Andrew White

    Andrew White August 20th, 2010 @ 12:56 AM

    • 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.

  • xHire

    xHire August 24th, 2010 @ 01:41 PM

    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.

  • Andrew White

    Andrew White August 24th, 2010 @ 01:51 PM

    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.

  • xHire

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages