Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
victorbstan opened this Issue · 9 comments

8 participants

@victorbstan

I have tried:

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

as well as:

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

@swrobel

Same issue here. I also tried:

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

@homakov
Collaborator

gem version? it was fixed

@swrobel

1.0.9

@Sylvain

Same for me with 1.0.9 version.

@tmlee

I have tried

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

They seem to work as expected

But,

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"})
@scouttyg

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" )

@zackshapiro

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

@GenesisFrame

Hi,

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.

Examples

auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")

=> "Go to http://www.rubyonrails.org and

say hello to david@loudthinking.com"

auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)

=> "Visit http://www.loudthinking.com/

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 david@loudthinking.com"

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 http://www.m....

  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 http://www.myblog.com.

  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 http://www.myblog.com.

  Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</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] || {}
    end
    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)
    end
  end
@GenesisFrame

hi,

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.