[英]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。
我想給href鏈接到靜態圖像
<%= link_to image_tag("user.png", class: "profile-img"), image_path("user.png"), class: "colorbox" %>
答案很好: Rails 4 link_to更大的靜態圖像
image_tag
幫助器將使用asset_path
查找要顯示的圖像。
因此,您可以從鏈接中使用asset_path("user.png")
或image_path("user.png")
中image_path("user.png")
。
這樣做的另一個好處是它會根據您的環境( production
與development
)而變化。 IE在生產中,它將首先查找您的預編譯資產。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.