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