如何将图像作为 href in link_to in ruby on rails

How to give image as href in link_to in ruby on rails

我的密码是

<%= link_to image_tag("user.png", class: "profile-img"), "/images/user.png", class: "colorbox" %>

如果我给

<%= link_to image_tag(current_user.avatar.url(:thumb), class: "profile-img"),current_user.avatar.url, class: "colorbox" %>

它工作正常,但在以前的情况下它找不到 user.png。

我想将 link 的 href 赋予静态图像

<%= link_to image_tag("user.png", class: "profile-img"), image_path("user.png"), class: "colorbox" %>

正确答案:Rails 4 link_to larger, static image


image_tag 助手将使用 asset_path 查找要显示的图像。

因此,您可能会受益于在 link 中使用 asset_path("user.png")image_path("user.png")

这样做的额外好处是它会根据您的环境而变化(productiondevelopment)。 IE 在生产中,它会首先查找您的预编译资产。