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

Ticket #6677 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] generated URL helpers ignore request's parameters

Reported by: mhw Assigned to: ulysses
Priority: normal Milestone: 1.2.7
Component: ActionPack Version: edge
Severity: normal Keywords: fd resources routes
Cc: matrix9180, carlivar, minam

Description

Given a request to a URL like this:

/products/42

product_path(@product) will generate the path '/products/42'

Given a request to a URL like this:

/brands/16/products/42

product_path(@product) will generate the path '/brands/42/products', which is unlikely to be what was wanted.

The 'classic' behaviour of url_for fills missing values from the request parameters, so given the same URL product_path(:id => @product) would generate the path '/brands/16/products/42'; it would be more consistent and more useful for product_path(@product) to work in the same way.

The current behaviour is due to the arguments being paired with dynamic segments in the path from left to right. It would be better to pair the arguments from right to left instead:

:brand_id:id
current@product.idnil
suggestednil@product.id

or more generally, given the URL /as/:a_id/bs/:b_id/cs/:c_id/ds/:id and the call to d_path(@c, @d):

:a_id:b_id:c_id:id
current@c.id@d.idnilnil
suggestednilnil@c.id@d.id

Attachments

routing-patch.diff (1.2 kB) - added by mhw on 11/22/06 10:10:09.
suggested patch
routing-test.diff (1.3 kB) - added by mhw on 11/23/06 20:29:41.
test case for unit test

Change History

11/22/06 10:10:09 changed by mhw

  • attachment routing-patch.diff added.

suggested patch

11/23/06 16:16:42 changed by david

  • owner changed from David to ulysses.

11/23/06 16:17:51 changed by david

  • keywords set to fd.

11/23/06 16:28:06 changed by ulysses

Can you please provide a failing test case? As a counter, here is a test case that looks like it should fail, but it doesn't:

  def test_named_route_should_not_choose_other_routes
    rs.draw do |map|
      map.connect '/wrong/:login', :controller => 'admin/user', :action => 'show'
      map.user '/admin/user/:login', :controller => 'admin/user', :action => 'show'
    end
    x = setup_for_named_route.new
    h = x.send(:user_path, :login => 'dhh')
    h.delete :only_path
    assert_equal '/admin/user/dhh', rs.generate(h)
  end

11/23/06 16:45:55 changed by ulysses

By the way, you might want to check and ensure that you do not have to named routes with the same name -- if you do, then the latter one will take precedence and lead to behavior such as this.

11/23/06 20:29:41 changed by mhw

  • attachment routing-test.diff added.

test case for unit test

11/23/06 20:44:30 changed by mhw

Ok, I've attached a unit test which sets up a set of routes for the /as/:a_id/bs/:b_id/cs/:c_id/ds/:id path in a similar way to a set of nested map.resources calls. It then uses the named route methods to generate a couple of routes with objects passed as parameters. The first assertion in the test fails because the URL generated is incorrect. The second assertion fails because the routing modules raises an exception.

11/24/06 05:50:15 changed by ulysses

Ok, I totally misunderstood before -- thanks for the test case. :-)

We need to decide if reversing the order is a special case, or if all positional parameters ought to be in reversed order.

I suspect the former is more the case. If so, then we can add support for an explicit order to be provided. Then we can update the resources call to specify whatever order we feel makes sense for simply restful.

Jamis -- does this sound good?

01/13/07 12:26:53 changed by matrix9180

  • cc set to matrix9180.

01/16/07 22:02:18 changed by carlivar

  • cc changed from matrix9180 to matrix9180, carlivar.

01/28/07 17:37:35 changed by ulysses

  • cc changed from matrix9180, carlivar to matrix9180, carlivar, minam.

02/27/07 06:57:28 changed by bitsweat

  • keywords changed from fd to fd resources routes.
  • milestone set to 1.2.

Bump!