我如何告诉 Rails/HAML 不要转义 URL?

发布于 2024-12-21 03:44:28 字数 1106 浏览 4 评论 0原文

我有这段代码:

= link_to "unsubscribe instantly", "*|UNSUB|*".html_safe

生成这个 HTML

<a href="*%7CUNSUB%7C*">unsubscribe instantly</a>

:字符被转义。这是行不通的,因为我将此 HTML 发送到一个服务,该服务应该用取消订阅 url 替换 *|UNSUB|*

相反,我希望 Rails/HAML 生成以下内容:

<a href="*|UNSUB|*">unsubscribe instantly</a>

我访问 http://haml-lang.com/ try.html 并输入 %a{:href =>; "*|UNSUB|*"} 取消订阅 输出正是我所期望的。所以我猜这是 Rails 的事情。


更新:我在新的 Rails 3.1 应用程序上尝试了这一点,并且管道没有被转义——这正是我想要的。我的主 Rails 应用程序发生了一些奇怪的事情,导致 URL 被转义——现在需要进一步研究它。


UPDATE: I figured it out. I had some Rack middleware that was running something like:

content = Nokogiri(response)
# ... processing
return content.to_html

这是对 URL 中的内容进行编码。我在这里问了一个相关问题:防止 Nokogiri 在 URL 中转义字符

I have this code:

= link_to "unsubscribe instantly", "*|UNSUB|*".html_safe

That generates this HTML:

<a href="*%7CUNSUB%7C*">unsubscribe instantly</a>

The | characters are escaped. That won't work, as I'm sending this HTML to a service that is supposed to replace *|UNSUB|* with an unsubscribe url.

Instead, I want Rails/HAML to generate this:

<a href="*|UNSUB|*">unsubscribe instantly</a>

I went to http://haml-lang.com/try.html and entered %a{:href => "*|UNSUB|*"} unsubscribe and the output was what I was expecting. So I'm guessing this is a Rails thing.


UPDATE: I tried this on a new Rails 3.1 application and the pipes aren't being escaped -- which is what I wanted. There's something weird happening with my main rails application that's causing the URLs to be escaped -- looking into it further now.


UPDATE:
I figured it out. I had some Rack middleware that was running something like:

content = Nokogiri(response)
# ... processing
return content.to_html

This was encoding the stuff inside the URLs. I asked a related question here: Preventing Nokogiri from escaping characters in URLs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泛滥成性 2024-12-28 03:44:28

这些真的是逃出来的吗?我刚刚使用rails 3.0.3(编辑:和rails 3.1.1)进行了测试,其中:

= link_to 'unsubscribe instantly', '*|UNSUB|*'   
%a{:href => '*|UNSUB|*'} unsubscribe instantly
:plain
  <a href="*|UNSUB|*">unsubscribe instantly</a>

然后我在页面上使用了curl,管道似乎在那里,如下所示:

curl http://localhost:3000/about | grep UNSUB
<a href="*|UNSUB|*">unsubscribe instantly</a>
<a href='*|UNSUB|*'>unsubscribe instantly</a>
<a href="*|UNSUB|*">unsubscribe instantly</a>

Are these really escaped? I just made a test with rails 3.0.3 (edit: and rails 3.1.1), with those:

= link_to 'unsubscribe instantly', '*|UNSUB|*'   
%a{:href => '*|UNSUB|*'} unsubscribe instantly
:plain
  <a href="*|UNSUB|*">unsubscribe instantly</a>

then I used curl on the page an the pipe seems to be there, as is:

curl http://localhost:3000/about | grep UNSUB
<a href="*|UNSUB|*">unsubscribe instantly</a>
<a href='*|UNSUB|*'>unsubscribe instantly</a>
<a href="*|UNSUB|*">unsubscribe instantly</a>
回首观望 2024-12-28 03:44:28

我想通了。我有一些运行类似以下内容的 Rack 中间件:

content = Nokogiri(response)
# ... processing
return content.to_html

这是对 URL 内的内容进行编码。

I figured it out. I had some Rack middleware that was running something like:

content = Nokogiri(response)
# ... processing
return content.to_html

This was encoding the stuff inside the URLs.

谈场末日恋爱 2024-12-28 03:44:28

您是否尝试过跳过 link_to 并直接创建链接标记,例如

a{:href=>"*|UNSUB|*"} unsubscribe instantly

Have you tried skipping the link_to and just to create the link tag directly like

a{:href=>"*|UNSUB|*"} unsubscribe instantly
行雁书 2024-12-28 03:44:28

这对你有用吗:(

=! link_to "unsubscribe instantly", "*|UNSUB|*"

意识到它或多或少是等价的,但可能会有所不同)

does this work for you:

=! link_to "unsubscribe instantly", "*|UNSUB|*"

(realise that it's more or less equivalent, but might just make the difference)

删除→记忆 2024-12-28 03:44:28

我没有地方可以测试这个,但这行得通吗?

- html = "<a href="*|UNSUB|*">unsubscribe instantly</a>"
= raw html

I don't have a place to test this but would this work?

- html = "<a href="*|UNSUB|*">unsubscribe instantly</a>"
= raw html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文