簡體   English   中英

如何在rails中的ruby中的link_to中將圖像作為href

[英]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")

這樣做的另一個好處是它會根據您的環境( productiondevelopment )而變化。 IE在生產中,它將首先查找您的預編譯資產。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM