Rails: Open link in new tab (with 'link_to') - ruby-on-rails

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?

The target: :_blank parameter should be a parameter of link_to, whereas you put it in image_tag parameters. Modify your code like this:
<%= 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 %>

Try this:
<%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook"), "http://www.facebook.com/mypage", :target => "_blank" %>

You can also use target: :_blank instead of 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 "http://www.facebook.com/mypage", target: :_blank do %>
<%= image_tag "facebook.png", class: "facebook_icon", alt: "Facebook" %>
<% end %>

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.
To prevent this one needs to add on the 'rel' attribute to the code.
rel: 'noopener'
Now the link_to should be:
<%= link_to image_tag("facebook.png", class: :facebook_icon, alt: "Facebook"), "http://www.facebook.com/mypage", target: :_blank, rel: 'noopener' %>
rubocop docs

If you're looking for how to open a link in a new tab within html (for anyone came here from Google), here:
Link name

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:
link_to(name = nil, options = nil, html_options = nil, &block)
Example:
link_to('Click me', { action: 'show', controller: 'blog', id: 1 }, { target: '_blank' })

Related

Outputting a Rails 5.0 Key Value Path as a String

I'm very new to Rails so I'll try and explain this the best I can. I have a simple Products model with one of the fields being the name of the Stripe plan name. This is so a new product can be added and the selection will append the plan name to the end of the path. It works great hard coded for testing.
Heres my products/show.html.erb line in questions. I am only including this for now as any experienced Rails developer should see the issue.
products/show.html.erb hard coded
...
<%= link_to 'Subscribe', new_subscription_path(plan: 'monthly'), class: 'btn btn-primary' %>
...
products/show.html.erb what I need
...
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
...
Having <%= #product.stripe_name %> in as a string throws an error.
The desired outcome would look like this in the browser
http://127.0.0.1:3000/subscription/new?plan=monthly
This:
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
Should be this:
<%= link_to 'Subscribe', new_subscription_path(plan: #product.stripe_name), class: 'btn btn-primary' %>
You don't put <%= ... %> inside another <%= ... %>.

How can I display a truncated URL with the link_to helper?

How would I truncate a url submitted from a form? Here is what I am using to display the submitted url:
index view:
<%= link_to gym.gym_website.try(:remove, /.*\/\//), url_for(gym.gym_website), target: '_blank', class: '' %>
How would I add the truncate method onto this? I am currently removing http/https from the submitted url.
<%= truncate(gym.gym_website, :escape => false, :length => 45) %>
Thanks in advance!
You could use the block version of the link_to helper for readability here.
<%= link_to url_for(gym.gym_website), target: '_blank', class: '' do %>
<%= truncate(gym.gym_website.try(:remove, /.*\/\//), :escape => false, :length => 45) %>
<% end %>

social site link that be open in another window in rails app

How to implement social sites link in rails app. Actually I did the following:
<%= link_to "www.linkedin.com/in/raj4057kumar", :target => "_blank", class: "linkedin" do %>
<%= image_tag("linkedin.png", :alt => "Linkedin") %>
<% end %>
But the problem is:
Url being: http://localhost:3000/www.linkedin.com/in/raj4057kumar
while we need like: www.linkedin.com/in/raj4057kumar
How to do this???
Its because you are not adding the http:// infront of the url.
Change your code to:
<%= link_to "LINK NAME HERE", "http://www.linkedin.com/in/raj4057kumar", :target => "_blank", class: "linkedin" do %>
<%= image_tag("linkedin.png", :alt => "Linkedin") %>
<% end %>
Try inserting https:// at the beginning for external url
<%= link_to 'Raj Kumar', 'https://www.linkedin.com/in/raj4057kumar', target: '_blank', class: 'linkedin' %>

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

<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?
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'}
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' }
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'}

Can't add class to Rails link_to

I tried to add a simple HTML class to my link_to tag:
<%= link_to "Individual Images", action: 'feature_highlights', site_title: site.title, class: "btn" %>
..but it gets appended to the URL instead:
How do I add a class to my link_to tag?
As per the API docs:
Be careful when using the older argument style, as an extra literal
hash is needed
So, the solution was
<%= link_to "Individual Images", { action: 'feature_highlights', site_title: site.title }, class: "btn" %>

Resources