Problems with should_be_restful and Rails > 1.2.3

2 views
Skip to first unread message

Tammer Saleh

unread,
Nov 28, 2007, 2:36:37 PM11/28/07
to sho...@googlegroups.com
It looks like they changed the way map.resources works with nested
resources at some point after 1.2.3.

This:

ActionController::Routing::Routes.draw do |map|
map.resources :users do |user|
user.resources :posts
end
end

Used to produce the named routes

user_url(user) and
post_url(user, post)

Now it automatically prepends a path prefix of user_ to the nested
resource, producing:

user_url(user) and
user_post_url(user, post)

The upshot is that should_be_restful, by default, will break for the
nested controller. You should be able to specify the redirections by
hand to get around this, but I thought it was worth bringing up.

I'm not sure if it's worth having shoulda look at the rails version to
fix this, or just jump to rails 2.0 for the shoulda 4.0 release.

Cheers,
Tammer
---------------------
www.tammersaleh.com
www.thoughtbot.com
tsa...@thoughtbot.com
(626) 841-0708
Usually online via Google Talk


James Golick

unread,
Nov 28, 2007, 2:45:00 PM11/28/07
to sho...@googlegroups.com
This patch roughly solves the problem of 2.0/edge/1.2.3+ routing for nested and namespaced (these are broken too) resources...

Index: vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb
===================================================================
--- vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb    (revision 319)
+++ vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb    (working copy)
@@ -239,17 +239,24 @@
             ensure_valid_members(@formats,        VALID_FORMATS, 'formats')
            
             @identifier    ||= :id
-            @klass         ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
+            @namespace     ||= target.name.split('::').slice(0..-2).map(&:underscore).map(&:to_sym)
+            @klass         ||= target.name.split('::').last.gsub(/ControllerTest$/, '').singularize.constantize
             @object        ||= @klass.name.tableize.singularize
             @parent        ||= []
             @parent          = [@parent] unless @parent.is_a? Array
 
             singular_args = @parent.map {|n| "@#{object}.#{n}"}
-            @destroy.redirect ||= "#{@object.pluralize}_url(#{singular_args.join(', ')})"
+           
+            namespace_pieces = [@namespace, @parent].flatten
+            @namespace_prefix = ''
+            @namespace_prefix << namespace_pieces.join('_')
+            @namespace_prefix << '_' unless @namespace_prefix.blank?
+           
+            @destroy.redirect ||= "#{@namespace_prefix}#{@object.pluralize}_url(#{singular_args.join(', ')})"
 
             singular_args << "@#{object}"
-            @create.redirect  ||= "#{@object}_url(#{singular_args.join(', ')})"
-            @update.redirect  ||= "#{@object}_url(#{singular_args.join(', ')})"
+            @ create.redirect  ||= "#{@namespace_prefix}#{@object}_url(#{singular_args.join(', ')})"
+            @update.redirect  ||= "#{@namespace_prefix}#{@object}_url(#{singular_args.join(', ')})"
             @denied.redirect  ||= "new_session_url"
           end
Reply all
Reply to author
Forward
0 new messages