Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7454 (new defect)

Opened 1 year ago

Last modified 5 months ago

[PATCH] map.resources for resources with uncountable names clobbers its own url generators

Reported by: jkit Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: uncountable resources route risky
Cc:

Description

Calling map.resources :users creates url generators for the collection (e.g., users_url) and url generators for members of the collection (e.g., user_url). However, for resources that have names which are uncountable, such as "sheep", the collection and member url generators result in a name clash. Ticket #7422 has a test case which demonstrates a bug due to this behavior. However, changeset [6062], which patches up the bug in ticket #7229, suggests that the solution cannot overload one method for both the collection and member url generators. Instead, the method names must differ for the collection url generator and the member url generator.

The patch that is attached, disambiguates the two by appending "_collection" to the resource name for the collection url generators. This results in sheep_collection_url for the sheep collection, and sheep_url for the member. Note that this change only applies to resources with names that are uncountable. Applying this change creates a need for a patch to the scaffold_resource generator to ensure that it calls the "_collection" versions when necessary.

Attachments

disambiguate_url_generators_for_uncountable_resources.patch (6.3 kB) - added by jkit on 02/01/07 10:42:49.
patch to fix bug exhibited by test case in ticket #7422

Change History

02/01/07 10:42:49 changed by jkit

  • attachment disambiguate_url_generators_for_uncountable_resources.patch added.

patch to fix bug exhibited by test case in ticket #7422

02/01/07 16:15:34 changed by hasmanyjosh

I'm not opposed to this patch, but I do find the solution less than ideal. It would break under a situation where route helper names were being generated programatically, as I do in some of my own code that can deal with arbitrary resource/model types. e.g.:

  path_helper = "#{request.path_parameters[:controller].to_s}_path".to_sym
  nav_menu = controller.menu_list.map { |m| link_to(m.camelize, send("#{m}_path")) }.join(" | ")

There are certainly other ways to do what those snippets are doing, but they all take more code, and they would break using your solution. It would be nice to see a solution that worked with existing naming conventions, though that might be impossible given the constraints.

03/15/07 18:22:08 changed by josh

  • keywords changed from uncountable resources route to uncountable resources route risky.

03/19/07 05:21:18 changed by protocool

There's already a way to disambiguate the generated url helpers - pass a :singular param to your map.resources call.

map.resources :fish, :singular => :fish_instance, :new => {:preview => :post}, :member => {:fillet => :post}

fish_path #=> /fish
fish_instance_path(1) #=> /fish/1
edit_fish_instance_path(1) #=> /fish/1;edit
new_fish_instance_path #=> /fish/new
preview_new_fish_instance_path #=> /fish/new;preview
fillet_fish_instance_path(1) #=> /fish/1;fillet

06/30/07 10:32:15 changed by nickretallack

I don't have a patch for you, but this is how I'd do it: Everywhere else in rails, singular and plural symbols seem to be interchangeable. Why not honor that convention here, and decide on whether the resource url is singular or plural simply by whether or not the caller passed in an ID argument?

12/19/07 06:18:37 changed by parcelbrat

Hasn't this been fixed elsewhere? Can we close it?