<%= button_to "Website", @restaurant.website, :target => '_blank', :class => "btn btn-primary" %> Everything works except " /> <%= button_to "Website", @restaurant.website, :target => '_blank', :class => "btn btn-primary" %> Everything works except " /> <%= button_to "Website", @restaurant.website, :target => '_blank', :class => "btn btn-primary" %> Everything works except "/>

How to open a link in a new window with ruby?

2.6k views Asked by At
<li class="li_spacing"><%= button_to "Website", @restaurant.website, :target => '_blank', :class => "btn btn-primary" %></li>

Everything works except for :target => '_blank'. Why does this not render the the link in a new page?

3

There are 3 answers

0
neydroid On

Change from button_to to a link_to:

<%= link_to "Website", @restaurant.website, target: "_blank" %>

button_to is generated inside a form, if you still wish to use it:

button_to "Website", @restaurant.website, class: 'btn btn-primary', form: {target: '_blank'}
2
spickermann On

A common way to open a link in a new window was to add a target="_blank" to the link tag.

But for security reasons this should not be done anymore. It is recommended to use rel=noreferrer instead:

link_to 'title', url, rel: 'noreferrer'

Or as a button:

button_to 'title', url, form: { rel: 'noreferrer' } 
0
Fakhir Shad On

If you are using button_to helper then it is automatically wrapped in a form by rails. So what you can do is to pass the attribute target: '_blank' as:

button_to "Website", @restaurant.website, class: 'btn btn-primary', form: {target: '_blank'}