1

I have a string of html in a rails helper module:

def app_link
  "<a href=\"https://itunes.apple.com/us/app/one-spark/id630800549?mt=8\" target=\"_blank\">
    <img src=\"/assets/apple-download-button.png\" alt=\"App Store\" />
  </a>"
end

When it renders normally it escapes the HTML, but when I add

<%= app_link.html_safe %>

The assets part of the image path is dropped and changed to:

<a href="https://itunes.apple.com/us/app/one-spark/id630800549?mt=8" target="_blank">
  <img src="apple-download-button.png" alt="App Store" />
</a>

I'm guessing this has something to do with the asset pipeline but it seems like really bizarre behaviour.

Travis Todd
  • 236
  • 2
  • 12
  • when using .html_safe does it redirect to correct image? – sansarp Apr 04 '15 at 08:46
  • no. correct imagepath is "/assets/apple-download-button.png" – Travis Todd Apr 04 '15 at 08:57
  • I think html_safe is not causing the issue, i have checked and its returning the correct image path i.e. "/assets/apple-download-button.png" – Amit Singh Apr 04 '15 at 08:59
  • After reading through this http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html I've got it rendering correctly with `raw app_link` but this doesn't help me debug this odd behaviour. – Travis Todd Apr 04 '15 at 09:03

1 Answers1

2

Try with this modified version of your helper. Hope it helps.

def app_link    
  app_link_html = ''
  app_link_html <<  "<a href=\"https://itunes.apple.com/us/app/one-spark/id630800549?mt=8\" target=\"_blank\"><img src=\"/assets/apple-download-button.png\" alt=\"App Store\" /></a>"
  app_link_html.html_safe
end 

and in view

<%= app_link %>
sansarp
  • 1,446
  • 11
  • 18
  • ok great. For html elements I often do in that way and is the preferred way to include html_safe method in the helper itself. – sansarp Apr 04 '15 at 09:53