Zander Perry
Zander Perry

Reputation: 99

Using link_to make an image the link

I need to link this image to profile_path(thing.user)

<%= image_tag thing.user.photo.url(:small), { :class=>"rounded_square" ,
    style: "margin-left:8px; text-align:center; vertical-align:middle;" } %>

Upvotes: 1

Views: 1836

Answers (3)

Denny Mueller
Denny Mueller

Reputation: 3615

You could try it with nested erb.

<%= link_to "#{image_tag(yourimagepath)}", "link_path" %>

Should look something like this.

     <%= link_to "#{image_tag thing.user.photo.url(:small), 
        { :class=>'rounded_square' , style: "margin-left:8px; text-align:center; vertical-align:middle;" }}", 
       "profile_path(thing.user)" %>

Upvotes: 0

Florian
Florian

Reputation: 3366

link_to should take a block if nothing helps:

<%= link_to profile_path(thing.user) do %>
    <%= image_tag thing.user.photo.url(:small), { :class=>"rounded_square" ,
style: "margin-left:8px; text-align:center; vertical-align:middle;" } %>
<% end %>

Upvotes: 4

Niels B.
Niels B.

Reputation: 6310

= link_to somewhere_path(some_object) do
  = image_tag some_object.url, class: 'rounded_square'
end

The return value of the block becomes the content for the a tag.

Upvotes: 1

Related Questions