Dantes Dantes - 1 year ago 109
Ruby Question

Rails: Open link in new tab (with 'link_to')

I have this code:

<%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook", :target => "_blank"),
"http://www.facebook.com/mypage" %>

How can I make it open in a new tab when a user clicks the link?

Answer Source

The :target => '_blank' parameter should be a parameter of link_to, whereas you put it in image_tag parameters. Modify your code like this:

<%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook"), "http://www.facebook.com/mypage", :target => "_blank" %>

Or with a block:

<%= link_to "http://www.facebook.com/mypage", :target => "_blank" do %>
  <%= image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook") %>     
<% end %>  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download