glennm
glennm

Reputation: 335

How to use "_blank" or "_new" in Rails

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/' %>

Upvotes: 21

Views: 27251

Answers (4)

Caleb Keene
Caleb Keene

Reputation: 388

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

Upvotes: 1

Brad
Brad

Reputation: 8678

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

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

Upvotes: 5

Ismael
Ismael

Reputation: 16730

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

Upvotes: 60

Ajey
Ajey

Reputation: 8202

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

Upvotes: 0

Related Questions