Ruby on Rails使用link_to和image_tag

时间:2021-12-18 00:27:25

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:

我希望类“work”是a href的类,而不是查询参数,因此它应该是:

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

is this possible?

这是可能的吗?

2 个解决方案

#1


32  

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.

你在哪里提供HREF,当有人点击图片时必须检索的路径?link_to很好,假设它是当前路径。理想情况下,您可以提供,path作为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.

您不应该依赖于空的散列作为第二个参数,而是显式地提供在单击图像时要访问的路径。

#2


5  

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

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

I'm using Rails 4 and this worked:

我使用的是Rails 4,这是可行的:

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

#1


32  

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.

你在哪里提供HREF,当有人点击图片时必须检索的路径?link_to很好,假设它是当前路径。理想情况下,您可以提供,path作为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.

您不应该依赖于空的散列作为第二个参数,而是显式地提供在单击图像时要访问的路径。

#2


5  

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

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

I'm using Rails 4 and this worked:

我使用的是Rails 4,这是可行的:

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