auto_link
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (1)
- 2.0.3 (13)
- 2.1.0 (0)
- 2.2.1 (14)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
auto_link(text, *args, &block)
public
Turns all URLs and e-mail addresses into clickable links. The :link option will limit what should be linked. You can add HTML attributes to the links using :html. Possible values for :link are :all (default), :email_addresses, and :urls. If a block is given, each URL and e-mail address is yielded and the result is used as the link text.
Examples
auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com") # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>" auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls) # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a> # or e-mail david@loudthinking.com" auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses) # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>" post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com." auto_link(post_body, :html => { :target => '_blank' }) do |text| truncate(text, 15) end # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>. Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
You can still use auto_link with the old API that accepts the link as its optional second parameter and the html_options hash as its optional third parameter:
post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com." auto_link(post_body, :urls) # => Once upon\na time # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>. Please e-mail me at me@email.com." auto_link(post_body, :all, :target => "_blank") # => Once upon\na time # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>. Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
Documentation bug
When adding the :target option, the documentation states that you should user :href_options like so:
auto_link(post_body, :href_options => { :target => '_blank' })
However, I could only get it to work using :html instead:
auto_link(post_body, :html => { :target => '_blank' })
I’m using Rails 2.2.2, but I believe that this also happens for more recent version .
Removed in 3.1.x
This method (and #auto_link_urls) has been removed in Rails 3.1 - other options are out there, such as Rinku, however there is a gem you can use for migration purposes etc, which is rails_autolink: http://rubygems.org/gems/rails_autolink