Ruby on Rails 使用 link_to 和 image_tag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4643698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:03:13  来源:igfitidea点击:

Ruby on Rails using link_to with image_tag

ruby-on-railsruby-on-rails-3

提问by AnApprentice

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 的类,而不是查询参数,因此它应该如下所示:

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

is this possible?

这可能吗?

回答by Aditya Sanghi

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 很友好并假设它是当前路径。理想情况下,您将提供路径作为 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.

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

回答by Tom

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

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

I'm using Rails 4 and this worked:

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

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