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?
What is the best way to get the current request URL in Rails? You should use request. original_url to get the current URL.
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.
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]"
In rails 3.1 auto_link has ben removed, its now a standalone gem: https://github.com/tenderlove/rails_autolink
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With