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 %>
Related
I have an image being pulled from the database in a table, that I would then like to make into a link so the viewer can be taken to the full article. I have seen that in theory I could use the below,
<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>
but in my case 'image/someimage.png' is
<%= image_tag coffeeshop.image_thumb_path %>
I've tried simply dropping the image tag section in, so it becomes <%= link_to image_tag('image_tag coffeeshop.image_thumb_path') + "Some text", some_path %> but this doesn't work. Is there a way to do this?
link_to(body, url, html_options = {})
The first argument in your link_to will be interpreted as the body of the tag, and then your url, but if you want to add more content inside the link_to you can open it and then close it, all inside will be interpreted this way:
<a href="#">
...
</a>
So you can try:
<%= link_to some_path do %>
<%= image_tag coffeeshop.image_thumb_path %>
<% end %>
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
I'm trying to have a Font Awesome icon be hyperlinked with the string of a Rails attribute.
I have tried this,
<%= link_to do %>
<i class="fa fa-link"><% school.website %></i>
<% end %>
this,
<i class="fa fa-link" href="<% school.website %>"></i>
and this:
<i class="fa fa-link" href="<%= school.website %>"></i>
among other variations, and can't seem to get the syntax right.
Would really appreciate some help with the syntax here, can't seem to find a specific answer to how linking with attributes works, only actual static text hrefs.
PS: I'm new to Rails / using Rails 4.2.
Try this:
<%= link_to ('<i class="fa fa-link"></i>').html_safe, desired_path %>
Considering school.website is a URL and you have included icons in CSS, the following will do:
<%= link_to(school.website) do %>
<i class="fa fa-link"></i>
<% end %>
Passing it as a block works too.
<%= link_to school.website do %>
<%= '<i class="fa fa-link"></i>'.html_safe %>
<% end %>
Assuming school.website is a URL that you would like to link to and display the link as well alongside the link icon from font awesome:
<%= link_to("<i class='fa fa-link'></i> #{school.website}".html_safe,school.website) %>
I am going to suggest something a little different.
I am personlly using glyphicons/fa a lot on my website, so I decided to create a little helper in application_helper.rb
application_helper.rb
def fa(glyph, text = nil)
html = "<i class=\"fa fa-#{glyph}\"></i>"
html << " #{text.to_s.html_safe}" if text
html.html_safe
end
And actually the correct syntax to use in views becomes either :
<%= link_to(school.website, class: "xxx") do %>
<%= fa('link') %>
<% end %>
OR (more compact)
<%= link_to(school.website, class: "xxx"){ fa('link') } %>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
try this:
<%= link_to(school.website) do %>
<%= fa_icon "fas link" %>
<% end %>
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
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 %>