0

I am using a decorator for an object called @menu to render a link in my view. I am also using Draper gem for my decorators.

In my decorator, I have something in the lines of:

def link
  link_to model.name + icon("external-link"), model.url
end

But when I call this in my view using @menu.link, the html tag for <i> will be literally displayed on the page, instead of being processed.

By the way, I can render the same html by using a helper from the font-awesome gem: icon("external-link"). But it will yield the same result. Could anyone show me how to properly render the html in this case?

mc9
  • 6,121
  • 13
  • 49
  • 87
  • you should use `html_safe` on the link method. Something like this `<%= @menu.link.html_safe %>` – Pramod Solanky Sep 29 '14 at 07:58
  • Remove quotation mark: (model.name + " – RedZagogulin Sep 29 '14 at 07:59
  • @RedZagogulin Sorry, that was a typo. I edited my question. – mc9 Sep 29 '14 at 08:01
  • @PamioSolanky I called `html_safe`, but I still see the raw html. I also tried to call `html_safe` within the method definition by doing `link_to (model.name + icon('external_link').html_safe, model.url)`. – mc9 Sep 29 '14 at 08:05
  • NOt sure why this should happen. You could also use `raw` like this `= raw @menu.link` ?? Let me know – Pramod Solanky Sep 29 '14 at 08:06
  • @PamioSolanky Thanks for your suggestion. But when I try '<%= raw @menu.link %>`, I still see the raw html. – mc9 Sep 29 '14 at 08:14
  • @PamioSolanky I resolved it by calling `html_safe` on `(model.name + icon('external-link)` altogether. (as per @Hannes's answer) – mc9 Sep 29 '14 at 08:18
  • @MikeC - perfect, more information about html_safe / raw / h methods can be find in this question http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html – Hannes Sep 29 '14 at 08:26

1 Answers1

0

If you are using font-awesome-sass gem it would be smart to use the #icon helper method. In your case:

def link_with_icon
  link_to (model.name + icon('external-link')).html_safe, model.url
end
Hannes
  • 2,451
  • 2
  • 18
  • 11