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

Ticket #3843 (closed defect: duplicate)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Fixing url_for to handle nested hashes

Reported by: redbeard@gmail.com Assigned to: ulysses
Priority: normal Milestone:
Component: ActionPack Version: 1.0.0
Severity: normal Keywords: Edge,url_for
Cc:

Description

There's a bug with url_for(..) mishandling hashes. This becomes especially relevant when using AJAX and trying to pass back some context data without using entire forms.

With this issue the URL:

/mycontroller/myaction/1?my_hash[a]=value_a&my_hash[b]=value_b&etc=1

Would be routed correctly to the controller method:

class my_controller < ApplicationController

  def controller_method
    // Routing works correctly so you can access
    // @params[:my_hash][:b] for example.
    render_text url_for(@params)
  end

end

But the output of url_for would be incorrect:

/mycontroller/myaction/1?my_hash=avalue_abvalue_b&etc=1

I traced the problem back to url_rewriter.rb in method rewrite_path(..). While it handled array input correctly, it failed to handle hashes.

The attached patch against edge rails fixes the issue by moving the code that translated from hash to path-elements to another method called in recursion. This patch also adds a test of this behaviour. Naturally all tests in the actionpack passed successfully with the patch applied.

Would appreciate any comments regarding this patch.

HTH, Tal Rotbart

Attachments

rails_url_rewriter.diff (2.8 kB) - added by redbeard@gmail.com on 02/15/06 17:41:21.
Patch of defect
rails_url_rewriter.2.diff (2.9 kB) - added by redbeard@gmail.com on 02/16/06 10:39:42.
Patch with fixed indentation and better testing
rails_url_rewriter.2.2.diff (2.9 kB) - added by redbeard@gmail.com on 02/16/06 10:41:43.
Fixed patch's indentation and better test

Change History

02/15/06 17:41:21 changed by redbeard@gmail.com

  • attachment rails_url_rewriter.diff added.

Patch of defect

02/15/06 21:03:28 changed by gabriel@gironda.org

http://dev.rubyonrails.org/ticket/3720

I already made a patch for this too. It looks like we both stumbled on this problem :)

02/15/06 21:43:40 changed by redbeard@gmail.com

Hi Gabriel, Seems so, but I like this/my solution better :) There are some problems with you patch, I'll comment over there.

02/16/06 00:27:21 changed by gabriel@gironda.org

Hi Tal!

It seems you're right in that your patch is faster, however, there's a few problems with your patch also:

1) The tests fail, because the parameters are coming out the other end in a different order

  1) Failure:
test_nested_hashes(UrlRewriterTests) [./test/controller/url_rewriter_test.rb:28]:
<"?a_key[b_key]=b_key&a_key[c_key]=c_value&a_key[d_key]=d_value&a_key[e_value][f_key]=f_value&a_key[e_value][g_key]=g_value"> expected but was
<"?a_key[e_value][f_key]=f_value&a_key[e_value][g_key]=g_value&a_key[b_key]=b_key&a_key[c_key]=c_value&a_key[d_key]=d_value">.

2) The indenting appears a little off in your patch also (at the end of build_elements, to be exact)

3) The patch must be applied with patch -p5 because of the file paths

Other than this, your solution is about twice as fast.

02/16/06 09:25:48 changed by redbeard@gmail.com

Hi Gabriel!

I'll check the indenting, thanks!

Also I'd be glad to hear a suggestion regarding the test. I'm thinking perhaps: *. url_for(nested_hash) -> string, *. parsing that string back into a hash using the routing parser *. Asserting equality on the source and result hash

Would that be a good test? -Tal

02/16/06 10:39:42 changed by redbeard@gmail.com

  • attachment rails_url_rewriter.2.diff added.

Patch with fixed indentation and better testing

02/16/06 10:41:43 changed by redbeard@gmail.com

  • attachment rails_url_rewriter.2.2.diff added.

Fixed patch's indentation and better test

02/16/06 10:44:41 changed by redbeard@gmail.com

Sorry about the double attachment. 2.diff and 2.2.diff are the same file.

Gabriel: I'd appreciate if you test it on your configuration. Remember to clean from previous patch first.

02/19/06 04:09:39 changed by david

  • owner changed from David to nseckar@gmail.com.
  • milestone deleted.

03/04/06 04:57:27 changed by ulysses

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

I'm marking this ticket duplicate and moving discussion to #3720 in the interest of having a single ticket for this issue.

Please reattach your patches to that ticket.

Thanks, Ulysses

03/04/06 05:00:29 changed by ulysses

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

*cough*

Closed the wrong ticket... Nothing to see here. Move along...

03/30/06 19:53:36 changed by redbeard@gmail.com

Any update regarding this?

08/01/06 23:18:15 changed by anonymous

For what it's worth, I had to use this patch to solve a pagination problem to pass params when using select_date. It worked great at resolving the flattening issue. Thanks for posting the patch. I hope they make this a part of the next rails release!

08/05/06 00:22:02 changed by me@julik.nl

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

I have dealt with the same thing in #4947, maybe this one might be closed?