简体   繁体   English

尽管以http://(Rails 2)开头,link_to外部URL被视为内部URL

[英]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 并且它与以下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. 出于某种原因,它不会将http://识别为指示外部URL,并将其视为相对URL。

I tried going to the source of link_to and copied it into my application helper. 我尝试使用link_to的源代码并将其复制到我的应用程序助手中。 I found that the .html_safe was doing the transformation, because when I pulled it out, it worked as expected. 我发现.html_safe正在进行转换,因为当我把它拉出来时,它按预期工作。

I can't figure out which html_safe its using. 我无法弄清楚它使用的是哪个html_safe When I copied in the source of HAML:Util.html_safe it also worked fine. 当我在HAML:Util.html_safe的源代码中复制时,它也运行良好。 Therefore, I assume the html_safe that link_to is using is resolving to something else, even though the view is a Haml file. 因此,我假设link_to正在使用的html_safe正在解析为其他内容,即使该视图是Haml文件。

Based on what I see in the Haml method, it seems that html_safe calls html_safe! 基于我在Haml方法中看到的,似乎html_safe调用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 我正在使用普通的美式英语/拉丁字符集,所以我不明白为什么它没有正确解析http://

I'm using Rails 2.3.5. 我正在使用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. 现在,我计划将href手动编码到我的视图中,以避免为外部链接调用link_to ,但如果有人能帮我弄清楚发生了什么,我会非常感激。 Thanks! 谢谢!

I'm unable to reproduce this behavior, but it sounds like a bug. 我无法重现这种行为,但听起来像是一个bug。 Also please note that Rails 2.3.5 is quite old. 另请注意,Rails 2.3.5已经很老了。 I believe the current 2.3.x version is 2.3.12. 我相信目前的2.3.x版本是2.3.12。

As a side note, the API docs you're referring to are for Rails 3.1.3. 作为旁注,您所指的API文档适用于Rails 3.1.3。 So you won't be able to obtain useful 2.3.5 debugging info from those docs. 因此,您将无法从这些文档中获取有用的2.3.5调试信息。 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. 读取应用程序中实际使用的源的最简单方法是查看开发计算机上的gems文件夹。

Anyhow, there's definitely nothing wrong with hand-coding the <a> tag like you're doing. 无论如何,像你正在做的那样对<a>标签进行手工编码绝对没有错。 In fact, it's slightly more efficient, as it avoids executing the Ruby code in link_to . 事实上,它稍微有点效率,因为它避免了在link_to执行Ruby代码。 So, it may actually be best just to keep doing what you're doing. 因此,实际上最好只是继续做你正在做的事情。

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. 我在ERB文件中遇到了同样的问题,当我使用而不是引用一个未加引号的链接并且之后调用html_safe时,我的问题就解决了。 Maybe your problem will be safed too. 也许你的问题也会得到保障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM