23

I have the following:

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), :class=>"work") %>

which outputs the following:

<a href="/xxxx/308?class=work"><img alt="xxxx" src="xxxxx"></a>

I want the class "work" to be a class for the a href not a query param, so it should look like:

<a href="/xxxx/308" class="work">

is this possible?

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

3 Answers3

38

Where are you providing the HREF, the path that must be retrieved when someone click's the image? link_to is being kind and assuming it to be the current path. Ideally you would provide, the path as the second option to link_to.

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), :class=>"work") %>

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>

You should not rely on an empty hash as the second parameter, but explicitly provide the path you want to go to when image is clicked.

Aditya Sanghi
  • 13,370
  • 2
  • 44
  • 50
6

The above answer didn't work for me. Maybe a different version of rails?

I'm using Rails 4 and this worked:

<%= link_to (image_tag (participant.user.profile_pic.url (:small)), class: 'work'), user %>
Tom
  • 2,543
  • 3
  • 21
  • 42
3

In Rails 6:

<%= link_to root_path do %>
  <%= image_tag("A_logo_black.png", alt: 'Logo',class: 'h-16')%>
<% end %>

where you have a root path defined in your routes.rb file:

Rails.application.routes.draw do
  root to: 'dashboard#index'
end
belgoros
  • 3,590
  • 7
  • 38
  • 76