1
<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?

Tobias
  • 4,523
  • 2
  • 20
  • 40

3 Answers3

3

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'}
neydroid
  • 1,913
  • 13
  • 14
2

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' } 
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • Thank you for your answer @spickermann. Will those security reasons apply only to user generated content? In other words, will target="_blank" be ok for links where the content will not be user generated? – BenKoshy Oct 09 '19 at 23:35
  • 1
    I would argue that the `rel: 'noreferrer'` protect the browser from malicious content no matter if it was user-generated or explicitly written by the website owner itself. The website owner might think that there is no malicious on their site and therefore the `noreferrer` is not needed. But from the user's point of view there is no difference. The user doesn't care if the malicious content was user-generated or explicitly placed by the website owner. Therefore IMHO just always use `malicious ` to indicate that you care about protecting the user. – spickermann Oct 10 '19 at 05:24
1

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'}
neydroid
  • 1,913
  • 13
  • 14
Fakhir Shad
  • 1,071
  • 8
  • 20