This project is archived and is in readonly mode.
[PATCH] Actions on singular resources do not generate route helpers in Rails 3.0
Reported by VirtualFunction | March 25th, 2010 @ 06:33 AM | in 3.0.2
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
Comments and changes to this ticket
-
VirtualFunction March 25th, 2010 @ 07:19 AM
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.
-
Andrew White March 25th, 2010 @ 01:29 PM
- 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.
-
Andrew White March 25th, 2010 @ 01:41 PM
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
-
Rizwan Reza March 25th, 2010 @ 05:13 PM
- 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.
-
Repository March 27th, 2010 @ 07:49 AM
- 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... -
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Medium
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>
People watching this ticket
Attachments
Referenced by
- 3817 When routing creates a singular resource, it doesn't add itself to name_prefix Duplicate of #4266. It's already fixed to master.
- 4266 [PATCH] Actions on singular resources do not generate route helpers in Rails 3.0 (from [39c35ff04b4478968b8994bc5fad74f5840eb64c]) Fix nam...