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.

[PATCH] Actions on singular resources do not generate route helpers in Rails 3.0

#4266

I'm guessing this is a little subjective, so I guess some might see this as a bug, some might see it as a feature. If it's not a bug, could something be done in the documentation to clarify how things should be done.

In Rails 2.x I could do something like

support.resource :item, :collection => { :mark_as_spam => :get }

this would provide me with something like: mark_as_spam_item_path

In Rails 3.0 this obviously translates to something like

resource :item do
  get :mark_as_spam
end

Which is fine, except I don't get a named path unless I do something like

resource :item do
  get :mark_as_spam, :as => :item
end

or

resource :item do
  # For multiple actions, scope seems to be the best way, not sure if this a truly public method in an 'API' sense though
  scope :as => :item do
    get :mark_as_spam
  end
end

Which makes this work as expected, but this seems rather convoluted and UN-DRY. Surely by default this should generate an as clause based on the resource name unless :as has been specified explicitly

Reported by VirtualFunction · March 25th, 2010 @ 06:33 AM

State: resolved
Milestone: 3.0.2
Assigned to: josh josh
Importance: Medium

Activity

  1. VirtualFunction
    VirtualFunction

    Some extra details FYI:

    I've just shifted from the Rails 3.0.0.beta package to the bleeding edge version (16a5e918a06649ffac24fd5873b875daf66212ad-master) and now find that I have to do:

      resource :item do
        get :mark_as_spam, :as => ''
      end
    

    otherwise the named route URL I get is mark_as_spam_item_item (item repeated twice).

    I'm guessing a lot of effort is getting focused on the dispatch mechanisms in Rails at the moment as the upgrade seems to increased the dispatch performance, or so it seems.

    March 25th, 2010 @ 07:19 AM

  2. Andrew White
    Andrew White
    • Tag changed from rails 3.0 beta, rails3 routes, rails3, routes, routing to rails 3.0 beta, rails3 routes, patch, rails3, routes, routing

    Simple patch attached which fixes this issue.

    March 25th, 2010 @ 01:29 PM

  3. Andrew White
    Andrew White

    Just in case the patch didn't make it due to LH current attachment issue here it is inline:

    From 3069f4a31d2fa5974229755b84eaa146fb67f4cb Mon Sep 17 00:00:00 2001
    From: Andrew White <andyw@pixeltrix.co.uk>
    Date: Thu, 25 Mar 2010 13:25:56 +0000
    Subject: [PATCH] Fix named routes for member actions of singleton resources
    
    ---
     actionpack/lib/action_dispatch/routing/mapper.rb |    2 +-
     actionpack/test/dispatch/routing_test.rb         |    5 +++++
     2 files changed, 6 insertions(+), 1 deletions(-)
    
    diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
    index ddee742..d120c95 100644
    --- a/actionpack/lib/action_dispatch/routing/mapper.rb
    +++ b/actionpack/lib/action_dispatch/routing/mapper.rb
    @@ -463,7 +463,7 @@ module ActionDispatch
               scope(:path => resource.name.to_s, :controller => resource.controller) do
                 with_scope_level(:resource, resource) do
     
    -              scope(:name_prefix => resource.name.to_s) do
    +              scope(:name_prefix => resource.name.to_s, :as => "") do
                     yield if block_given?
                   end
     
    diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
    index e0500af..65857f5 100644
    --- a/actionpack/test/dispatch/routing_test.rb
    +++ b/actionpack/test/dispatch/routing_test.rb
    @@ -26,6 +26,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
     
           resource :session do
             get :create
    +        post :reset
     
             resource :info
           end
    @@ -240,6 +241,10 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
           get '/session/edit'
           assert_equal 'sessions#edit', @response.body
           assert_equal '/session/edit', edit_session_path
    +
    +      post '/session/reset'
    +      assert_equal 'sessions#reset', @response.body
    +      assert_equal '/session/reset', reset_session_path
         end
       end
     
    -- 
    1.6.4.4
    

    March 25th, 2010 @ 01:41 PM

  4. Rizwan Reza
    Rizwan Reza
    • Milestone cleared.
    • State changed from new to verified
    • Assigned user set to josh
    • Title changed from Actions on signular resources in Rails 3.0 do not create a named route helper method to [PATCH] Actions on singular resources do not generate route helpers in Rails 3.0

    +1 The patch is needed to fix the functionality. Applies cleanly.

    March 25th, 2010 @ 05:13 PM

  5. Repository
    Repository
    • State changed from verified to resolved

    (from [39c35ff04b4478968b8994bc5fad74f5840eb64c]) Fix named routes for member actions of singleton resources [#4266 state:resolved]

    Signed-off-by: wycats wycats@gmail.com
    http://github.com/rails/rails/commit/39c35ff04b4478968b8994bc5fad74...

    March 27th, 2010 @ 07:49 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Medium

    October 15th, 2010 @ 11:01 PM