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/' %>
It is ok to use target="_blank" ; This was done away with in XHTML because targeting new windows will always bring up the pop-up alert in most browsers. XHTML will always show an error with the target attribute in a validate.
a target=”_blank” Open in New Browser Tab (or Window) The target attribute specifies where the linked document will open when the link is clicked. The default is the current window. If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.
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
For anyone wondering how to achieve this when passing a block:
<%= link_to(product.link, target: '_blank') do %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With