【问题标题】:How to give image as href in link_to in ruby on rails如何在 ruby​​ on rails 的 link_to 中将图像作为 href
【发布时间】:2015-10-22 07:48:54
【问题描述】:

我的代码是

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

我想给一个静态图片的链接href

【问题讨论】:

  • 图片在你的文件结构中的什么位置?
  • assets/images/user.png 中的图片
  • 您使用的是回形针,对吗?问题:如果包含回形针,为什么要使用静态链接?

标签: ruby-on-rails ruby ruby-on-rails-4 asset-pipeline


【解决方案1】:
<%= 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 查找要显示的图像。

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

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

【讨论】:

  • asset_path("user.png") 有效:)。你能告诉我黑白asset_path和image_path的区别吗?谢谢
  • 我认为image_path 只是在/images 目录中查找,而asset_path 在所有目录中查找。我真的不敢说实话!
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多