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.

nested resource routes not generated correctly (rails 2.3)

#3198

In rails 2.3 nested resource routes are not generated correctly. In particular, nestings show up as query params instead of nesting path segments.

This behaviour is not present in 2.2 and earlier.

Example:

config/routes.rb:

  map.resources :posts
  map.resources :authors, :has_many => :posts

In a Rails 2.3 console (incorrect behaviour):

  >> include ActionController::UrlWriter
  >>
  >> url_for :controller => 'posts', :action => 'index', :author_id => '1', :only_path => true
  => "/posts?author_id=1"

In a rails 2.2 console (the correct behaviour):

  >> include ActionController::UrlWriter
  >>
  >> url_for :controller => 'posts', :action => 'index', :author_id => '1', :only_path => true
  => "/authors/1/posts"

If the map.resources :posts line is removed from the routes file, then the correct result is returned. This leads me to believe that this is something to do with the algorithm that maps params to routes. Has this changed in 2.2. > 2.3? (Just looking for a place to start debugging)

Reported by Ian White · September 14th, 2009 @ 01:23 PM

State: invalid
Milestone: 2.3.6
Assigned to: josh josh
Importance: High

Activity

  1. Sam Ruby
    Sam Ruby

    I've verified that this happens in Rails 2.3.4, but not in Rails 2.3.3.

    September 17th, 2009 @ 04:55 PM

  2. Tom Stuart
    Tom Stuart

    Yeah, this is broken.

    FWIW you can turn on the old routing behaviour by commenting out the ActionController::Routing.generate_best_match = false line in config/initializers/new_rails_defaults.rb, which Josh introduced in commit 6fdfe4cb5fa85160f5d91bad64d5c23b4f87f820.

    September 17th, 2009 @ 05:05 PM

  3. Michael Koziarski
    Michael Koziarski
    • Milestone set to 2.3.6
    • Assigned user set to josh

    Josh,

    this is all you

    September 17th, 2009 @ 10:12 PM

  4. josh
    josh
    • State changed from new to open

    @Ian does using the 2.x generation behavior fix this issue? Or is it present in both cases?

    September 18th, 2009 @ 03:49 AM

  5. Tom Stuart
    Tom Stuart

    Yes, using the 2.x generation behaviour (ActionController::Routing.generate_best_match = true) fixes the problem.

    September 18th, 2009 @ 06:51 AM

  6. Ian White
    Ian White

    @Josh, as Tom says, using generate_best_match fixes it. As does flipping the order of the route declaration.

    Is this expected Rails 3 behaviour?

    September 18th, 2009 @ 08:32 AM

  7. josh
    josh
    • State changed from open to invalid

    Correct.

    "ActionController::Routing.generate_best_match = false" is the default option for new projects since it is going to be the only option in Rails 3.

    September 18th, 2009 @ 03:19 PM

  8. Tom Stuart
    Tom Stuart

    Which is why this ticket is valid: the breakage occurs when ActionController::Routing.generate_best_match = false.

    September 18th, 2009 @ 03:22 PM

  9. Tom Stuart
    Tom Stuart

    Oh, I see, it's actually meant to work like this. Never mind.

    September 18th, 2009 @ 03:33 PM

  10. Sam Ruby
  11. josh
    josh

    @Sam, this patch has not been applied to master since the current router will be completely rewritten. The motivation for this switch in 2.x is to easy the pain of upgrading to the new router. Rails 3.0 will not have this flag. All routes will be generated identical to the "ActionController::Routing.generate_best_match = false" behavior.

    I'm still in the progress of integrating the new router into master. I expect a few more weeks of work are left.

    September 18th, 2009 @ 07:09 PM