Ruby on Rails使用link_to和image_tag

AnA*_*ice 19 ruby-on-rails ruby-on-rails-3

我有以下内容:

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), :class=>"work") %>
Run Code Online (Sandbox Code Playgroud)

输出以下内容:

<a href="/xxxx/308?class=work"><img alt="xxxx" src="xxxxx"></a>
Run Code Online (Sandbox Code Playgroud)

我希望类"work"成为一个href而不是查询参数的类,所以它看起来像:

<a href="/xxxx/308" class="work">
Run Code Online (Sandbox Code Playgroud)

这可能吗?

Adi*_*ghi 35

你在哪里提供HREF,当有人点击图像时必须检索的路径?link_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") %>
Run Code Online (Sandbox Code Playgroud)

您不应该依赖空哈希作为第二个参数,而是明确提供单击图像时要转到的路径.


Tom*_*Tom 6

上述答案对我不起作用.也许是不同版本的rails?

我正在使用Rails 4,这有效:

<%= link_to (image_tag (participant.user.profile_pic.url (:small)), class: 'work'), user %>
Run Code Online (Sandbox Code Playgroud)