This project is archived and is in readonly mode.

#4567 ✓invalid
Vikrant Chaudhary

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

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

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.

Comments and changes to this ticket

  • Vikrant Chaudhary

    Vikrant Chaudhary May 10th, 2010 @ 11:53 AM

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

  • Andrew White

    Andrew White June 7th, 2010 @ 07:26 AM

    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.

  • DHH

    DHH June 8th, 2010 @ 08:36 PM

    • State changed from “new” to “invalid”

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