0

I want to convert URL in text to <a> as HTML.
The following code causes <a> to be escaped in erb.
How do I fix?

index.html.erb

<p>
  <%= urlify('Hello https://stackoverflow.com/') %>
  #=> Hello <a href="https://stackoverflow.com/">https://stackoverflow.com/</a>
</p>

helper.rb

require 'uri'

def urlify(text)
  URI.extract(text, ['http', 'https']).uniq.each do |url|
    text = text.gsub!(url, link_to(url, url))
  end
end
kusumoto_teruya
  • 2,415
  • 4
  • 23
  • 38

1 Answers1

0

you can do like this:

<p>
  <%= raw(urlify('Hello https://stackoverflow.com/')) %>
</p>

Happy Coding :-)

Palash Bera
  • 696
  • 5
  • 12