This project is archived and is in readonly mode.

#1560 ✓stale
Dan Pickett

link_to in ActionView::TestCase uses ActionController::UrlRewriter's url_for instead of ActionView::Helpers::UrlHelper

Reported by Dan Pickett | December 11th, 2008 @ 01:28 PM | in 2.3.10

I'm not sure if I'm omitting something from my test that I should be, but I couldn't find documentation around how to resolve this issue.


class SomeHelperTest < ActionView::TestCase
  def test_a_link
    link_to "Home", root_url
  end
end

Stack trace will return


TypeError: can't convert String into Hash
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/url_rewriter.rb:130:in `merge'
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/url_rewriter.rb:130:in `url_for'
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/url_helper.rb:228:in `link_to'

Shouldn't it be using the more robust url_for in ActionView::Helpers::UrlHelper?

Thanks

Comments and changes to this ticket

  • Dan Pickett

    Dan Pickett December 11th, 2008 @ 02:11 PM

    close this ticket please, I had some old code in my test_helper.

    Thanks, Dan

  • Jeremy Kemper

    Jeremy Kemper December 12th, 2008 @ 12:32 AM

    • State changed from “new” to “invalid”

    cheers :)

  • theflow

    theflow July 2nd, 2009 @ 05:05 PM

    I'm having problems with this and haven't found a way to work around it in all cases.

    In earlier versions of Rails (I'm coming from 2.1) link_to did this:

    url = case options
      when String
        options
      when :back
        @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
      else
        self.url_for(options)
      end
    

    whereas now it just does a unconditional

    url = url_for(options)
    

    So, if you include

    ActionController::UrlWriter

    somewhere and do a link_to with a named route you get the nice "TypeError: can't convert String into Hash" exception, because url_for in UrlWriter doesn't know how to handle Strings.

    Would love to work on a patch, but I have no idea how this is supposed to work:

    • just change the link_to implementation?
    • change UrlWriter#url_for to handle Strings as options?
    • I'm doing something wrong?

    thanks,
    Florian

  • Michael Koziarski

    Michael Koziarski July 5th, 2009 @ 03:57 AM

    • State changed from “invalid” to “open”

    change UrlWriter#url_for to handle Strings as options?

    This seems like the right place to handle this for me.

  • Michael Koziarski

    Michael Koziarski July 5th, 2009 @ 03:58 AM

    At the same time it could be worthwhile attempting to rationalise the 3 different implementations of url_for to make sure that they code they share, isn't repeated.

    I don't expect that you'll avoid having a few different implementations of url_for but the implementations should only differ in the ways that matter.

  • Courtenay

    Courtenay August 25th, 2009 @ 01:54 AM

    I'm seeing this too. Basically it means you can't use named/restful routes in your own classes (say, a presenter class).

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Dan Pickett

    Dan Pickett May 9th, 2010 @ 05:23 PM

    • Tag set to bugmash
  • elcuervo

    elcuervo May 15th, 2010 @ 09:56 PM

    not reproducible , in master UrlWriter#url_for is currently supporting String as options.

    actionpack/lib/action_dispatch/routing/url_for.rb:126

    def url_for(options = nil)
      case options
      when String
        options
      when nil, Hash
        _router.url_for(url_options.merge((options || {}).symbolize_keys))
      else
        polymorphic_url(options)
      end
    end
    
  • Enrico Bianco

    Enrico Bianco May 16th, 2010 @ 12:07 AM

    +1, not reproducible in master.

    The url_for method in ActionView::Helpers::UrlHelper also handles strings (complete with new auto-escaping functionality):
    (actionpack/lib/action_view/helpers/url_helper.rb:98)

    def url_for(options = {})
      options ||= {}
      url = case options
      when String
        escape = true
        options
      when Hash
        options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
        escape  = options.key?(:escape) ? options.delete(:escape) : false
        super
      when :back
        escape = false
        controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
      else
        escape = false
        polymorphic_path(options)
      end
    
      escape ? escape_once(url).html_safe : url
    end
    
  • Rohit Arondekar

    Rohit Arondekar June 15th, 2010 @ 11:57 AM

    • Assigned user set to “José Valim”

    Can somebody verify if this issue is still reproducible? Or else the ticket can be closed.

  • José Valim

    José Valim June 15th, 2010 @ 12:01 PM

    • Assigned user cleared.
  • Rohit Arondekar

    Rohit Arondekar June 21st, 2010 @ 09:06 AM

    • State changed from “open” to “resolved”

    I can't reproduce it in master either. Closing ticket assuming resolved.

  • patrick collins

    patrick collins June 21st, 2010 @ 09:27 PM

    in rails 2.3.5

    If I do:

    class Test
      
      include ActionView::Helpers
      include ActionController::UrlWriter
      
      default_url_options[:host] = "localhost:3000"
      
      def foo
        link_to "click me", "www.google.com"
      end
      
    end
    
    ...
    
    Test.new.foo
    

    I get:

    can't convert String into Hash

    Edited by Rohit Arondekar to fix formating.

  • Rohit Arondekar

    Rohit Arondekar June 22nd, 2010 @ 02:16 AM

    • Milestone changed from 3.x to 2.x
    • State changed from “resolved” to “needs-more-info”

    Patrick, can you please try this on Rails 2.3.8 or even the 2-3-stable branch?

  • Neeraj Singh

    Neeraj Singh July 29th, 2010 @ 09:22 PM

    • Milestone changed from 2.x to 2.3.9
    • Importance changed from “” to “Low”
  • Jeremy Kemper

    Jeremy Kemper August 30th, 2010 @ 02:28 AM

    • Milestone changed from 2.3.9 to 2.3.10
  • Christopher Dell

    Christopher Dell November 23rd, 2010 @ 11:56 AM

    I can reproduce this bug on a clean 2.3.9 app adding Patrick's Test class to lib/test.rb and calling <%= Test.new.foo %> from a view.

    Found this bug after trying to use link_to in a Presenter like court3nay alluded to.

  • rails

    rails February 24th, 2011 @ 12:00 AM

    • State changed from “needs-more-info” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails February 24th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Tags

Pages