method

auto_link

Importance_3
v1.1.6 - Show latest stable - 2 notes - Class: ActionView::Helpers::TextHelper
auto_link(text, link = :all, href_options = {}, &block) public

Turns all urls and email addresses into clickable links. The link parameter can limit what should be linked. Options are :all (default), :email_addresses, and :urls.

Example:

  auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com") =>
    Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and
    say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>

If a block is given, each url and email address is yielded and the result is used as the link text. Example:

  auto_link(post.body, :all, :target => '_blank') do |text|
    truncate(text, 15)
  end
Show source
Register or log in to add new notes.
August 6, 2009
4 thanks

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 .

November 6, 2011 - (>= v3.1.0)
3 thanks

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