开发者

Ruby on Rails using link_to with image_tag

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 sh开发者_开发知识库ould look like:

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

is this possible?


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜