Use an image path from database as link_to - ruby-on-rails

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 %>

Related

How to use the tags and classes in link_to helper

Here is the code for my link:
<%=link_to "My Stations", retailer_my_stations_path%>
I want to style the link like:
<a class="dropdown-collapse" href="#"><i class='icon-edit'></i>
<span>My Stations</span>
</a>
How do I make Rails' link_to helper use the Icon <i> tag and the "My Stations" be inside the <span> tag like in the theme?
You can pass a block to link_to:
<%= link_to retailer_my_stations_path, class: 'dropdown-collapse' do %>
<i class='icon-edit'></i>
<span>My Stations</span>
<% end %>
You can also create a helper method for this.
link_to can optionally take a block. You can add all html_options in the html_options key. I believe (but am not positive) that you reference the class directly.
I just use html_options because it always works:
<% link_to(link_to "My Stations", retailer_my_stations_path, html_options: {class: "drop-down-collapse"} ) do %>
other tags i.e. span
<% end %>

How to add IMG with in ANCHOR while using link_to in Rails?

I want to create a link(HTML Anchor Tag) that contains an img Tag element in my application. I used link_to to produce links but don't know how to generate HTML tags within them.
Do i need to use javascript/jQuery rather than trying it in my application. Any suggestions would be appreciated.
Here it should look like
<a href='/groceries'>
<img src="location" alt="Groceries.jpg">
</a>
You can find this in documentation, anchor seems as:
<%= link_to "Comment wall", profile_path(#profile, anchor: "wall") %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
For your case:
<%= link_to groceies_path(anchor: 'header') do %>
<%= image_tag('test.png', alt: 'Grocieries') %>
<% end %>

Use an image from the asset pipeline inside of a link_tag

I want to create a link that uses an image from the asset pipeline. Image is stored at assets/images/github_icon.png.
Link tag without the image:
<%= link_to "Log in with Github", omniauth_authorize_path(resource_name, provider) %>
Link tag with the image (how do I create this one?):
<%= link_to ?????, omniauth_authorize_path(resource_name, provider) %>
In the past I've used raw() when adding html within a link tag:
<%= link_to raw("<p>Hey kids!</p>"), ... %>
But I was unsure how I could use raw() with a img_tag("github-icon.png").
link_to
Two ways:
<%= link_to image_tag("github_icon.png"), path %>
or
<%= link_to path do %>
<%= image_tag "github_icon.png" %>
<% end %>
--
Like many Rails helpers, link_to is very flexible - you just need to pass the right arguments to it. You're trying to pass img_tag (which doesn't exist as a method), and raw() (which won't do anything).
You should use image_tag, and won't need to use raw
Use link tag with block. Try
<%= link_to omniauth_authorize_path(resource_name, provider) do %>
Log in with Github
<%= image_tag "github_icon.png" %>
<% end %>
Notice link_to block version doesn't take text so you'll have to add your text Log in with Github inside block
You can try this method:
link_to image_tag("imageName.png", :border => 0), {:action => 'actionName', :controller => 'controllerName'}, {:class => 'className'}
Or you can try this:
<%= link_to href: '/url/for/you' do %>
<%= image_tag 'yourImage.jpg', width: 136, height: 67, alt: 'WIN!'%>
<% end %>

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