Ruby-on-rails Rails:在新选项卡中打开链接(使用“link_to”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12133894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Open link in new tab (with 'link_to')
提问by Dantes
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?
当用户单击链接时,如何使其在新选项卡中打开?
回答by Baldrick
The target: :_blankparameter should be a parameter of link_to, whereas you put it in image_tagparameters. Modify your code like this:
该 target: :_blank参数应该是一个参数link_to,而你把它的image_tag参数。像这样修改你的代码:
<%= 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 %>
回答by Alexander Giraldo
Try this:
尝试这个:
<%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook"), "http://www.facebook.com/mypage", :target => "_blank" %>
回答by Deepak Mahakale
You can also use target: :_blankinstead of target: '_blank'
您也可以使用target: :_blank代替target: '_blank'
<%= link_to image_tag("facebook.png", class: "facebook_icon", alt: "Facebook"), "http://www.facebook.com/mypage", target: :_blank %>
link_to do
link_to do
<%= link_to "http://www.facebook.com/mypage", target: :_blank do %>
<%= image_tag "facebook.png", class: "facebook_icon", alt: "Facebook" %>
<% end %>
回答by funfuntime
If you're looking for how to open a link in a new tab within html (for anyone came here from Google), here:
如果您正在寻找如何在 html 中的新标签页中打开链接(对于来自 Google 的任何人),请点击此处:
<a href="http://www.facebook.com/mypage" target="_blank">Link name</a>
回答by Manuel
My understanding is: you can ask the browser to open a new tab or a new site. But this depends on the user settings. I considere this question answered.
我的理解是:你可以要求浏览器打开一个新标签或一个新站点。但这取决于用户设置。我认为这个问题已经回答了。
Except I fell in a trap when it is necessary to seperate the link options from the html options:
除了当需要将链接选项与 html 选项分开时,我陷入了困境:
link_to(name = nil, options = nil, html_options = nil, &block)
link_to(name = nil, options = nil, html_options = nil, &block)
Example:
例子:
link_to('Click me', { action: 'show', controller: 'blog', id: 1 }, { target: '_blank' })
回答by moody_drew
To add on to the previous answer the format below is what is being suggested by rubocop. This can be a security risk as the loaded page will have control over the previous page and could change its location for phishing purposes.
要补充上一个答案,下面的格式是 rubocop 建议的格式。这可能是一个安全风险,因为加载的页面将控制上一个页面,并可能出于网络钓鱼目的更改其位置。
To prevent this one needs to add on the 'rel' attribute to the code.
为了防止这种情况,需要在代码中添加“rel”属性。
rel: 'noopener'
Now the link_to should be:
现在 link_to 应该是:
<%= link_to image_tag("facebook.png", class: :facebook_icon, alt: "Facebook"), "http://www.facebook.com/mypage", target: :_blank, rel: 'noopener %>

