'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/' %>
Solution 1:[1]
For anyone wondering how to achieve this when passing a block:
<%= link_to(product.link, target: '_blank') do %>
Solution 2:[2]
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
Solution 3:[3]
you can also do target: :_blank if you prefer to use a symbol
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brad |
| Solution 2 | Ajey |
| Solution 3 | Caleb Keene |
