Skip to content


Repository files navigation



This is an extraction of the auto_link method from rails. The auto_link method was removed from Rails in version Rails 3.1. This gem is meant to bridge the gap for people migrating.


By default auto_link returns sanitized html_safe strings. This behaviour can be overridden by setting the :sanitize option to false (thus making it insecure if you don't have the content under control).


Add this line to your application's Gemfile:

gem 'rails_autolink'

And then execute:

$ bundle install


Basic Usage

require 'rails_autolink'

auto_link("Go to and say hello to")
# => "Go to <a href=\"\"></a> and
#     say hello to <a href=\"\"></a>"

Convert Only URLs to Links

auto_link("Visit or e-mail", :link => :urls)
# => "Visit <a href=\"\"></a>
#     or e-mail"

Convert Only Email Addresses to Links

auto_link("Visit or e-mail", :link => :email_addresses)
# => "Visit or e-mail <a href=\"\"></a>"

Generate Links Without Sanitizing HTML Tags

## By default, HTML tags are sanitized to protect from malicious code
auto_link("Go to <script>Malicious code!</script>")
# => "Go to <a href=\"\"></a> "

## Use the :sanitize => false option to prevent sanitization
auto_link("Go to <script>alert('Script!')</script>", :sanitize => false)
# => "Go to <a href=\"\"></a> <script>alert('Script!')</script>"

Customize Links and Shorten Text

post_body = "Welcome to my new blog at  Please e-mail me at"
auto_link(post_body, :html => { :target => '_blank' }) do |text|
  truncate(text, :length => 15)
# => "Welcome to my new blog at <a href=\"\" target=\"_blank\">http://www.m...</a>.


  • rails > 3.1