Ruby-on-rails 如何在 Rails 中使用“_blank”或“_new”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10290335/
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
How to use "_blank" or "_new" in Rails
提问by glennm
In HTML, if I wanted a link to open in a new window, I'd adopt target="_blank"like this:
在 HTML 中,如果我想在新窗口中打开一个链接,我会采用这样的target="_blank":
<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):
如何将“_blank”添加到导轨?这是我到目前为止的链接代码(但它目前在同一个选项卡/窗口中打开):
<%= link_to image_tag("img.png"), 'http://www.website.com/' %>
回答by Ismael Abreu
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
请参阅http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
回答by Brad
For anyone wondering how to achieve this when passing a block:
对于任何想知道如何在传递块时实现这一点的人:
<%= link_to(product.link, target: '_blank') do %>
<%= link_to(product.link, target: '_blank') do %>
回答by Caleb Keene
you can also do target: :_blankif you prefer to use a symbol
target: :_blank如果您更喜欢使用符号,您也可以这样做
回答by Ajey
you can remove the default action of the link in js as
您可以将 js 中链接的默认操作删除为
$('#button-id').click(function(e){
e.preventDefault();
});
The preventDefault() function prevents the default action of the event
preventDefault() 函数阻止事件的默认操作

