Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#1560

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

Reported by Dan Pickett · December 11th, 2008 @ 01:28 PM

State: stale
Milestone: 2.3.10
Assigned to: nobody
Importance: Low

Activity

  1. Dan Pickett
    Dan Pickett

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

    Thanks, Dan

    December 11th, 2008 @ 02:11 PM

  2. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to invalid

    cheers :)

    December 12th, 2008 @ 12:32 AM

  3. theflow
    theflow

    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

    July 2nd, 2009 @ 05:05 PM

  4. Michael Koziarski
    Michael Koziarski
    • 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.

    July 5th, 2009 @ 03:57 AM

  5. Michael Koziarski
    Michael Koziarski

    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.

    July 5th, 2009 @ 03:58 AM

  6. Courtenay
    Courtenay

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

    August 25th, 2009 @ 01:54 AM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  8. Dan Pickett
  9. elcuervo
    elcuervo

    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
    

    May 15th, 2010 @ 09:56 PM

  10. Enrico Bianco
    Enrico Bianco

    +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
    

    May 16th, 2010 @ 12:07 AM

  11. Rohit Arondekar
    Rohit Arondekar
    • Assigned user set to José Valim

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

    June 15th, 2010 @ 11:57 AM

  12. José Valim
    José Valim
    • Assigned user cleared.

    June 15th, 2010 @ 12:01 PM

  13. Rohit Arondekar
    Rohit Arondekar
    • State changed from open to resolved

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

    June 21st, 2010 @ 09:06 AM

  14. patrick collins
    patrick collins

    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.

    June 21st, 2010 @ 09:27 PM

  15. Rohit Arondekar
    Rohit Arondekar
    • 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?

    June 22nd, 2010 @ 02:16 AM

  16. Neeraj Singh
    Neeraj Singh
    • Milestone changed from 2.x to 2.3.9
    • Importance changed from to Low

    July 29th, 2010 @ 09:22 PM

  17. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  18. Christopher Dell
    Christopher Dell

    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.

    November 23rd, 2010 @ 11:56 AM

  19. rails
    rails
    • 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.

    February 24th, 2011 @ 12:00 AM

  20. rails
    rails
    • State changed from open to stale

    February 24th, 2011 @ 12:00 AM