我如何告诉 Rails/HAML 不要转义 URL?
我有这段代码:
= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些真的是逃出来的吗?我刚刚使用rails 3.0.3(编辑:和rails 3.1.1)进行了测试,其中:
然后我在页面上使用了curl,管道似乎在那里,如下所示:
Are these really escaped? I just made a test with rails 3.0.3 (edit: and rails 3.1.1), with those:
then I used curl on the page an the pipe seems to be there, as is:
我想通了。我有一些运行类似以下内容的 Rack 中间件:
这是对 URL 内的内容进行编码。
I figured it out. I had some Rack middleware that was running something like:
This was encoding the stuff inside the URLs.
您是否尝试过跳过
link_to
并直接创建链接标记,例如Have you tried skipping the
link_to
and just to create the link tag directly like这对你有用吗:(
意识到它或多或少是等价的,但可能会有所不同)
does this work for you:
(realise that it's more or less equivalent, but might just make the difference)
我没有地方可以测试这个,但这行得通吗?
I don't have a place to test this but would this work?