Kumar
Kumar

Reputation: 741

using url in a string in ruby

I want to convert urls in a string to workable urls. For example: str = "Hello, this is the link for yahoo: http://www.yahoo.com"

//in the view i have
<%= parse_description(r[:description]) %>

//in helper, i am splitting each word in the string and verifying if i have a string which
//  contains http, if http is present then i am using link_to to make it a valid url:
def parse_description(str)
  s = ""
    str.split.each do |w|
       a =  w.include?("http") ? (link_to nil,"#{w}") : w
       s += "#{a} "
    end 
end

once the string is returned back to the view, the link is not clickable. what wrong i am doing?

Upvotes: 0

Views: 87

Answers (2)

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22296

To match the URI's in a String use the following code:

html_string.scan(URI.regexp) do |*matches|
  p $&
end

Upvotes: 0

Kumar
Kumar

Reputation: 741

Thanks pst for your help.. while returning i just did s.html_safe.. it worked.

Upvotes: 1

Related Questions