Rick Heller
Rick Heller

Reputation: 81

link_to external URL treated as internal despite starting with http:// (Rails 2)

I'm using

link_to "Click Here", "http://www.cnn.com"

and its producing a link with the following href

"localhost:3000http://www.cnn.com"

For some reason, it's not recognizing the http:// as indicating an external URL, and treating it as a relative URL.

I tried going to the source of link_to and copied it into my application helper. I found that the .html_safe was doing the transformation, because when I pulled it out, it worked as expected.

I can't figure out which html_safe its using. When I copied in the source of HAML:Util.html_safe it also worked fine. Therefore, I assume the html_safe that link_to is using is resolving to something else, even though the view is a Haml file.

Based on what I see in the Haml method, it seems that html_safe calls html_safe! to transform the string when it thinks its unsafe.

I'm using the normal American English/Latin character set, so I don't see why it's not parsing the http:// correctly

I'm using Rails 2.3.5.

For now, I plan to hand-code the href into my view to avoid calling link_to for the external link, but if anyone can help me figure out what is going on, I'd greatly appreciate it. Thanks!

Upvotes: 3

Views: 1733

Answers (2)

tingel2k
tingel2k

Reputation: 392

I had the same problem within an ERB file, my problem was solved when I used instead of an quoted an unquoted link and called html_safe afterwards. Maybe your problem will be safed too.

Upvotes: 0

rlkw1024
rlkw1024

Reputation: 6515

I'm unable to reproduce this behavior, but it sounds like a bug. Also please note that Rails 2.3.5 is quite old. I believe the current 2.3.x version is 2.3.12.

As a side note, the API docs you're referring to are for Rails 3.1.3. So you won't be able to obtain useful 2.3.5 debugging info from those docs. The easiest way to read the source that's actually in use in your app is to look in the gems folder on your development machine.

Anyhow, there's definitely nothing wrong with hand-coding the <a> tag like you're doing. In fact, it's slightly more efficient, as it avoids executing the Ruby code in link_to. So, it may actually be best just to keep doing what you're doing.

Upvotes: 2

Related Questions