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.

Passing a single object to a nested resource route generates confusing urls.

#4567

I'll take an example. (Using Rails 2.3.5, Ubuntu 10.04 x64, Ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux])

#app/models/author.rb
class Author < ActiveRecord::Base
  has_many :books
end
#app/models/book.rb
class Book < ActiveRecord::Base
  belongs_to :author
end
#config/routes.rb
map.resources :authors do |author|
  author.resources :books
end
#app/controllers/authors_controller.rb
class AuthorsController < ApplicationController
end
<%# app/views/authors/show.html.erb %>
<%# Let's say Author#1 has 5 Book#1,#2,#3,#4,#5. %>
<% Author.first.books.each { |book| %>
  <%= edit_author_book_path(book) %> <%# I know two argument should have been passed. %>
<% } %>

Now visit - /authors/1, it generates -

  /authors/1/books/1/edit 
  /authors/2/books/1/edit 
  /authors/3/books/1/edit 
  /authors/4/books/1/edit 
  /authors/5/books/1/edit

While it should have either generated following or should have raised an error.

  /authors/1/books/1/edit 
  /authors/1/books/2/edit 
  /authors/1/books/3/edit 
  /authors/1/books/4/edit 
  /authors/1/books/5/edit

Please note that this bug is very specific, i.e., it happens only when you visit /authors/1 page. On rest of the pages it will generate ActionController::RoutingError.

Reported by Vikrant Chaudhary · May 10th, 2010 @ 11:44 AM

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

Activity

  1. Vikrant Chaudhary
    Vikrant Chaudhary

    I expect edit_author_book_path(book) to generate /authors/1/books/4/edit because I guess book also carries an Author object via book.author, so we shouldn't really be passing Author object anyways (or it should be optional at least).

    May 10th, 2010 @ 11:53 AM

  2. Andrew White
    Andrew White

    What's happening is that url generation is using the single argument as the :author_id and then getting the :id parameter from the existing url parameters. This is expected - the url generation code can't know that :id should be used as :author_id and the book argument should be :id.

    June 7th, 2010 @ 07:26 AM

  3. DHH
    DHH
    • State changed from new to invalid

    June 8th, 2010 @ 08:36 PM