Don P
Don P

Reputation: 63647

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").

Upvotes: 0

Views: 101

Answers (3)

Mandeep
Mandeep

Reputation: 9173

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

Upvotes: 1

Chrissx
Chrissx

Reputation: 357

You can try this method:

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

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76774

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

Upvotes: 3

Related Questions