- Add the following to
app/helpers/application_helper.rb
:
def external_link_to(name = nil, options = nil, html_options = nil, &block)
opts = { target: '_blank', rel: 'nofollow noopener' }
if block_given?
options ||= {}
options = options.merge(opts)
else
html_options ||= {}
html_options = html_options.merge(opts)
end
link_to(name, options, html_options, &block)
end
- Use it like so in a view template:
<%= external_link_to 'Github', 'https://github.com/' %>
Or, with html options like a class (also wrapping an image tag):
<%= external_link_to image_tag('github.svg'), 'https://github.com/', class: 'icon-link' %>
More info on the extra link attributes here: https://developers.google.com/web/tools/lighthouse/audits/noopener