20

In HTML, if I wanted a link to open in a new window, I'd adopt target="_blank" like this:

<a href="http://www.website.com/" target="_blank"><img src="/img.png" /></a>

How do I add the "_blank" to rails? Here's the code I so far for the link (but it currently opens in the same tab/window):

<%= link_to image_tag("img.png"), 'http://www.website.com/' %>
halfer
  • 19,824
  • 17
  • 99
  • 186
glennm
  • 325
  • 1
  • 5
  • 12

4 Answers4

59

I think it's like this

<%= link_to image_tag('img.png'), 'http://www.website.com', target: '_blank' %>

See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
  • 1
    That worked... Thanks! **target: "_blank"** also worked. I'm assuming this other version only works on recent versions of rails (I'm using 3.2.2). – glennm Apr 24 '12 at 05:14
4

For anyone wondering how to achieve this when passing a block:

<%= link_to(product.link, target: '_blank') do %>

Brad
  • 8,044
  • 10
  • 39
  • 50
0

you can remove the default action of the link in js as

$('#button-id').click(function(e){
  e.preventDefault();      
});

The preventDefault() function prevents the default action of the event

Ajey
  • 7,924
  • 12
  • 62
  • 86
0

you can also do target: :_blank if you prefer to use a symbol

Caleb Keene
  • 379
  • 2
  • 7