Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Rails 3 view, how do I detect an URL in a display string and format it as a link to that URL?

I have user generated comments on my site. If a user adds an URL in their comment, I'd like it to be formatted as a link and actually link to that URL. How do I do that?

like image 774
Sam Grossberg Avatar asked Feb 09 '11 08:02

Sam Grossberg


People also ask

What is the best way to get the current request URL in Rails?

What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.

How do I get the current URL in Ruby on Rails?

You can write request. url instead of request. request_uri . This combines the protocol (usually http://) with the host, and request_uri to give you the full address.


2 Answers

Rails has an auto_link text helper.

auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
#     say hello to <a href=\"mailto:[email protected]\">[email protected]</a>"

auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls)
# => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
#     or e-mail [email protected]"
like image 64
Simone Carletti Avatar answered Oct 17 '22 06:10

Simone Carletti


In rails 3.1 auto_link has ben removed, its now a standalone gem: https://github.com/tenderlove/rails_autolink

like image 28
nikstep Avatar answered Oct 17 '22 06:10

nikstep