2

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

Community
  • 1
  • 1
Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73
  • Did you tryed `!= link_to("unsubscribe instantly", "*|UNSUB|*".html_safe).html_safe` ? [post](http://stackoverflow.com/questions/5619577/html-is-being-escaped-in-link-to) – Mark Huk Dec 14 '11 at 20:42

5 Answers5

1

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>
Thibaut Barrère
  • 8,845
  • 2
  • 22
  • 27
  • Weird. I used curl to test, and the pipes are still being escaped. I'll try on a new rails application. – Joe Van Dyk Dec 14 '11 at 21:02
  • On a new Rails application, the URLs aren't being escaped (which is what I want). I must have some different configurations or gems in the other Rails application I was using to test. – Joe Van Dyk Dec 14 '11 at 21:07
1

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.

Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73
0

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

a{:href=>"*|UNSUB|*"} unsubscribe instantly
Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
0

does this work for you:

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

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

Chris Bailey
  • 4,126
  • 24
  • 28
0

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

- html = "<a href="*|UNSUB|*">unsubscribe instantly</a>"
= raw html
Webjedi
  • 4,677
  • 7
  • 42
  • 59