Skip to content

can't seem to get :target => "_blank" to work #20

victorbstan opened this Issue Aug 10, 2012 · 9 comments

8 participants


I have tried:

auto_link("text", :all, :target => "_blank")

as well as:

auto_link("text", :html => {:target => '_blank'})

swrobel commented Aug 14, 2012

Same issue here. I also tried:

auto_link("text", :all, :html => {:target => "_blank"})

homakov commented Aug 15, 2012

gem version? it was fixed

swrobel commented Aug 16, 2012


Sylvain commented Nov 22, 2012

Same for me with 1.0.9 version.

tmlee commented Mar 3, 2013

I have tried

auto_link("text", :all, :target => "_blank")
auto_link("text", :html => {:target => '_blank'})

They seem to work as expected


auto_link("text", :all, :html => {:target => "_blank"})

does not because its not getting extracted correctly in the auto_link implementation
should you want to pass both :all and :html, you could do so with

auto_link("text", :link => :all, :html => {:target => "_blank"})

If you guys are using simple_format with auto_link, watch out -- I think simple_format strips the target out of links. So, if before you were doing something like:

simple_format(auto_link(text, :target => "_blank" ))

Reverse it and you should be able to keep your options for auto_link, eg:

auto_link(simple_format(text), :target => "_blank" )


This isn't working for me either. Using 1.1.5

Tried auto_link("text", :link => :all, :html => {:target => "_blank"}), and auto_link("text", :all, :html => {:target => "_blank"})

Anyone have any luck? Not using simple_format in my project



This method is deprecated or moved on the latest stable version. The last existing version (v3.0.9) is shown here.

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.


auto_link("Go to and say hello to")

=> "Go to and

say hello to"

auto_link("Visit or e-mail", :link => :urls)

=> "Visit

or e-mail"

auto_link("Visit or e-mail", :link => :email_addresses)

=> "Visit or e-mail"

post_body = "Welcome to my new blog at Please e-mail me at"
auto_link(post_body, :html => { :target => '_blank' }) do |text|
truncate(text, 15)

=> "Welcome to my new blog at http://www.m....

  Please e-mail me at <a href=\"\"></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 Please e-mail me at"

auto_link(post_body, :urls) # => Once upon\na time

=> "Welcome to my new blog at

  Please e-mail me at"

auto_link(post_body, :all, :target => "_blank") # => Once upon\na time

=> "Welcome to my new blog at

  Please e-mail me at <a href=\"\"></a>."

See basic code here:

File actionpack/lib/action_view/helpers/text_helper.rb, line 302

  def auto_link(text, *args, &block)#link = :all, html = {}, &block)
    return '' if text.blank?

    options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
    unless args.empty?
      options[:link] = args[0] || :all
      options[:html] = args[1] || {}
    options.reverse_merge!(:link => :all, :html => {})

    case options[:link].to_sym
      when :all                         then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block)
      when :email_addresses             then auto_link_email_addresses(text, options[:html], &block)
      when :urls                        then auto_link_urls(text, options[:html], options, &block)


I think this helps you out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.