Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails using link_to with image_tag

Tags:

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?

like image 944
AnApprentice Avatar asked Jan 10 '11 04:01

AnApprentice


2 Answers

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.

like image 131
Aditya Sanghi Avatar answered Nov 09 '22 05:11

Aditya Sanghi


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 %> 
like image 28
Tom Avatar answered Nov 09 '22 04:11

Tom