Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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/' %>
like image 941
glennm Avatar asked Apr 24 '12 00:04

glennm


People also ask

Why target _blank is deprecated?

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.

What does the reserved target name _blank signify?

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.


2 Answers

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

like image 105
Ismael Avatar answered Sep 28 '22 02:09

Ismael


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

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

like image 28
Brad Avatar answered Sep 28 '22 02:09

Brad