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

Ticket #6432 (reopened defect)

Opened 3 years ago

Last modified 1 year ago

[PATCH] Polymorphic URLs for nested routes

Reported by: mhw Assigned to: David
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: simply_helpful actionpack polymorphic_routes
Cc:

Description

As discussed on rails-core, this patch adds support for passing an array of objects to form_for. The last element of the array is the object the form is being generated for; the preceding elements are passed through to the named route generator.

Attachments

simply_helpful-nested_routes.diff (8.3 kB) - added by mhw on 10/18/06 12:07:38.
suggested patch
polymorphic_nested_routes.patch (10.7 kB) - added by gravelpup on 06/05/07 19:01:36.
This patch relative to rails_root (old version was relative to actionpack)
form_helper_fix.patch (3.7 kB) - added by gravelpup on 06/06/07 15:49:01.
Patch for edge that fixes the form_helper bug
form_helper_fix_redux.patch (0.9 kB) - added by gravelpup on 06/06/07 21:20:54.
Another small fix
polymorphic_url_fixes_etc.patch (5.5 kB) - added by gravelpup on 06/07/07 19:38:32.
Tests and fixes for polymorphic_url and form_helper

Change History

10/18/06 12:07:38 changed by mhw

  • attachment simply_helpful-nested_routes.diff added.

suggested patch

05/26/07 01:26:46 changed by josh

Simply Helpful has been moved into Core. So can you please rewrite this for ActionPack. Please reopen once you done so.

05/26/07 01:26:53 changed by josh

  • status changed from new to closed.
  • resolution set to wontfix.

06/05/07 15:50:44 changed by gravelpup

  • keywords changed from simply_helpful to simply_helpful actionpack polymorphic_routes.
  • status changed from closed to reopened.
  • type changed from defect to enhancement.
  • resolution deleted.
  • summary changed from [PATCH] simply_helpful form_for does not handle nested routes to [PATCH] Polymorphic URLs for nested routes.

As requested, here's a patch rewritten for ActionPack. Usage is as follows:

# Routes.rb
map.resources :workshops do |w|
  w.resources :sessions
end

# View
<%= url_for([@workshop, @session]) %>
<%= link_to('Session', [@workshop, @session]) %>
<% form_for([@workshop, @session] do |f| %>
# ...
<% end %>

Original discussion on core is here.

06/05/07 18:07:34 changed by tobi

For consistency this method of polymorphism should also support route namespaces. Familiar symbol notation could be used here:

<%= url_for([:admin, @session]) %>
<%= link_to('Session', [:admin, @session]) %>
<% form_for([:admin, @session] do |f| %>

06/05/07 19:01:36 changed by gravelpup

  • attachment polymorphic_nested_routes.patch added.

This patch relative to rails_root (old version was relative to actionpack)

06/05/07 19:11:02 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6951]) Resources: url_for([parent, child]) generates /parents/1/children/2 for the nested resource. Likewise with the other simply helpful methods like form_for and link_to. Closes #6432.

06/06/07 15:48:09 changed by gravelpup

  • status changed from closed to reopened.
  • type changed from enhancement to defect.
  • resolution deleted.

apply_form_for_options! was calling polymorphic_path with invalid arguments. Tests passed anyway because form_helper_test mocks out polymorphic_path.

Here's a patch for the bug (against edge).

06/06/07 15:49:01 changed by gravelpup

  • attachment form_helper_fix.patch added.

Patch for edge that fixes the form_helper bug

06/06/07 16:52:41 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6959]) Fix incomplete work from [6951] that was hidden by test stubs. Closes #6432.

06/06/07 21:20:54 changed by gravelpup

  • attachment form_helper_fix_redux.patch added.

Another small fix

06/06/07 21:21:23 changed by gravelpup

  • status changed from closed to reopened.
  • resolution deleted.

Found and fixed another little bug.

06/06/07 23:10:26 changed by bitsweat

Missing test for last fix.

06/07/07 19:37:46 changed by gravelpup

Added unit tests and fixes for polymorphic_url. Removed stubbed polymorphic_url method in form_helper_test.rb.

06/07/07 19:38:32 changed by gravelpup

  • attachment polymorphic_url_fixes_etc.patch added.

Tests and fixes for polymorphic_url and form_helper

06/07/07 19:45:13 changed by gravelpup

This latest patch should also fix #8601.

06/07/07 21:35:05 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6960]) More nested polymorphic url helper fixes. Closes #6432, references #8601.

06/28/07 19:14:49 changed by bitsweat

  • status changed from closed to reopened.
  • resolution deleted.

[6960] drops other hash options, like :format, on the floor.

06/28/07 21:46:39 changed by gbuesing

#8782 handles the situation where :format is passed in to the options hash of polymorphic_url -- in this case, 'formatted_' is added to the beginning of the named route, and the desired format is added to the end of the args array.

03/04/08 23:56:17 changed by jasoo24

I found a bug with the way form_for([@blog,@comment]) works. It makes the incorrect assumption that the path to use is: blog_comment_path.

However, it is possible to change a path's prefix in routes.rb:

map.resources :blogs do |blog|
  blog.resources :comments, :name_prefix => nil
end

By setting :name_prefix to nil, this changes the comment path from the default, blog_comment_path, to just comment_path. This has the unfortunate side-effect of breaking form_for since it relies on the previously noted assumption.