Rails link_to with font-awesome and external url - ruby-on-rails

I'm trying to link to a youtube page in a rails app with the font-awesome gem.
<i><%= link_to fa_icon "youtube-square 2x", #book.youtube %></i>
In doing so, I receive no implicit conversion of Symbol into String error message.
Do I need to remove the font-awesome helper and include it as a class, or is there another way to make this work together?

Looking at the docs, I believe that you need to put the fa_icon helper in a link_to block:
<%= link_to #book.youtube do %>
<%= fa_icon "youtube-square 2x" %>
<% end %>

Related

How can I display the link only if I'm not already on taht page?

I'm using Rails 4.2.7. I have this link in the header of my application
<%= link_to 'Edit', edit_users_path %>
How would I not render this link if I'm already on the edit_users_path page?
You can put the condition at the end of the erb fragment
<%= link_to('Edit', edit_users_path) unless current_page? %>
just call
<% if current_page?(edit_users_path) %>

How to add http link on image tag using rails?

I am using ruby 2 and rails 4. I want to add http link into image link in rails. How can I create that?
My codes:
<% for g in #pictures %>
<%= link_to image_tag g.pic_url, class: "img-responsive img-thumbnail" %>
<% end %>
I want to create something like below using rails.
<img src="/assets/image_001.jpg" class="img-responsive img-thumbnail">
Please share with me if any one has any idea.
The link_to helper can take a block of code, allowing you to do something like the following:
<% for g in #pictures %>
<%= link_to g.pic_url do %>
<%= image_tag g.pic_url, class: "img-responsive img-thumbnail" %>
<% end %>
<% end %>
More info on the link_to helper can be found by looking through the Rails API documentation.
Hope it helps!
my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
link_to take first argument as link's name and second argument is as url. in your code you have not assign url option. Change it as below
Try:
<%= link_to (image_tag(g.pic_url, class: "img-responsive img-thumbnail"), g.pic_url) %>
Ruby on Rails using link_to with image_tag

Ruby on rails: dynamic code not working into erb

Here is my code
Here i want to include youtube video id dynamically. Its not taken here Kindly give me a solution
<%= link_to image_tag("http://img.youtube.com/vi/<%= video.provider_uid %>/hqdefault.jpg"), "http://www.facebook.com" %>
You need string interpolation here, not ERB tag:
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.facebook.com" %>
I would rewrite the code like that:
<%= link_to "http://www.example.com" do %>
<%= image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg") %>
<% end %>
I found a solution
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.example.com" %>
this is the code

How should we combine the div with link_to in Rails?

My original intention was to display some text on the image. At the same time, when we click the images, the webpage will be redirected.
And I use link_to function with div containing background image.
The code is like this:
<%= link_to raw('<div style="background-image:url(<%= image_url '1.jpg'%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
But the system tells me there is SyntaxError.
You can pass link_to a block that contains the content you want to display. So instead of going with the link_to(display, url, options={}) you get link_to(url, option={}, &block) where you can do.
<%= link_to index_by_dvp_domain_path do %>
<div style="background-image: url(<%= image_url '1.jpg'%>);width:340px;">
This is a test
</div>
<% end %>
After you do this you can treat it like normal html.
As always, I'd recommend trying to move any style out into it's own separate stylesheet.
Best way to do it this is used following
<%= link_to index_by_dvp_domain_path do
content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" )
end
%>
OR
<%= link_to content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" ), index_by_dvp_domain_path %>
Please have a try with
<%= link_to raw('<div style="background-image:url(#{image_url '1.jpg'}%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
I think using Link_to as below would be much more simpler even when you have a big block including multiple tags:
<%= link_to desired_path do %>
<div class="linkable">
<another div>
... some other tags
</another div>
</div>
<% end %>
and I recommend you to use a different background color for mouse over events because it shows the viewer that it's a link!
In you .css file:
.linkable:hover{
background-color: red;
}
Im so surprised to see that no one came up with the regular way of doing it in Rails.
<%= link_to image_tag("/images/1.jpg",:size => "340X340"),index_by_dvp_domain_path %>

Combining raw HTML and a rails variable in a link_to

I've got a link_to that looks like this
<%= link_to site.name, site %>
I want to add a font icon from bootstrap into the anchor text, but when I try to use raw() for that, I can't figure out the syntax for including the site.name hook.
This is what I'm trying:
<%= link_to raw("<i class="icon-hdd"></i> site.name"), site %>
That's not working. How do I change that line to make it work?
Have you tried the do syntax?
<%= link_to site do %>
<i class="icon-hdd"> </i> <%= site.name%>
<% end %>
You need to interpolate site_name into the string.
<%= link_to raw("<i class='icon-hdd'></i> #{site.name}"), site %>

Resources