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.

Wrong redirect after creation of nested singleton resource (using responder)

#4077

Hi,

I have a Rails 3.0.0.beta app with a singular :approval resource nested in a plural :posts resource. Now after creation of an approval, the responder attempts to redirect to '/posts/1/approval.1', not '/posts/1/approval'. Below are the relevant parts of the app

Cheers,
Ingo

config/routes.rb

resources :posts, :controller => 'posts' do
resource :approval, :controller => 'post_approval' end

app/controllers/post_approval_controller.rb

def create
@post = Post.find(params[:post_id]) @approval = @post.create_approval(params[:approval]) respond_with @post, @approval end

development.log

Started POST "/posts/1/approval" for 127.0.0.1 at 2010-03-01 16:09:30
Processing by PostApprovalController#create as HTML Parameters: {"commit"=>"Save Approval", "authenticity_token"=>"y1bI8bz+1fdAAI3vA3pCiKdoqJHvEijgGGGpkZMTL+Y=", "post_id"=>"1"} Post Load (0.3ms) SELECT "posts". FROM "posts" WHERE ("posts"."id" = 1) LIMIT 1 Approval Load (0.2ms) SELECT "approvals". FROM "approvals" WHERE ("approvals".post_id = 1) LIMIT 1 SQL (0.5ms) INSERT INTO "approvals" ("created_at", "post_id", "updated_at") VALUES ('2010-03-01 15:09:30.089291', 1, '2010-03-01 15:09:30.089291') Redirected to http://localhost:3000/posts/1/approval.1
Completed in 25ms with 302

Reported by Ingo · March 1st, 2010 @ 03:16 PM

State: wontfix
Milestone: 3.0.2
Assigned to: nobody
Importance: High

Activity

  1. José Valim
    José Valim
    • Assigned user set to José Valim
    • Milestone cleared.

    The problem is not in responder, but in url generation. Try doing this:

    url_for [@post, @approval]
    

    What does it return you?

    March 3rd, 2010 @ 08:45 AM

  2. Ingo
    Ingo

    yes, the above returns '/posts/1/approval.1'

    March 3rd, 2010 @ 12:22 PM

  3. José Valim
    José Valim
    • Assigned user changed from José Valim to josh
    • State changed from new to open

    March 3rd, 2010 @ 12:24 PM

  4. josh
    josh
    • Assigned user cleared.

    March 6th, 2010 @ 07:17 PM

  5. jonshea (at expandrive)
    jonshea (at expandrive)

    There’s a very similar test case in polymorphic_routes_test.rb. The method test_nesting_with_array_ending_in_singleton_resource tests the result of polymorphic_url([@project, :bid]), where Project is a plural resource and Bid is a singular resource. This test passes with the expected result, http://example.com/projects/16/bid.

    If the test is modified to use [@project, @bid] instead of [@project, :bid] then it fails as described above (http://example.com/projects/16/bid.17).

    I don’t know if perhaps it’s “wrong” for some reason to use an object for the singular resource instead of the symbol, but I’ve attached a patch for the modified test case regardless. If this is a real issue, then I’d be happy to look into the fix as well.

    April 13th, 2010 @ 07:51 PM

  6. José Valim
    José Valim
    • State changed from open to wontfix

    @Jonshea, this is actually a known bug, since early Rails versions. We know how to reproduce it, but the solution is quite difficult, since polymorphic_url/url_for would have to ask the router how to mount it.

    April 13th, 2010 @ 09:09 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM

  8. Brendon
    Brendon

    +1 This happens even when the singular resource is earlier in the array.

    March 11th, 2011 @ 08:17 AM