[英]Can not show the image by image_tag in rails
我正在嘗試制作一個非常簡單的 Rails 程序來顯示圖片,這是我的show.html.erb
文件
<div class="row"> <div class="col m12">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<div class="row">
<div class="col m4 center">
<%= image_tag "courses/#{@course.image}" %>
</div>
<div class="col m8">
<span class="card-title"><%= @course.title %></span>
<p><%= @course.description %></p>
</div>
</div>
</div> </div></div></div>
我想要的是,例如,在課程文件夾中,有 4 張圖片 1 2 3 4,當我連接到http://localhost:3000/courses/2
時,它會顯示圖片 2,但它給了我這個問題The asset "courses/image" is not present in the asset pipeline.
我很困惑我試圖修復它但不能。 我是新手,還在stydying,你能給我一些想法嗎? 非常感謝你。
您需要的不同方式是:
<%= image_tag "courses/1.png" %>
<%= image_tag "courses/2.png" %>
<%= image_tag "courses/3.png" %>
<%= image_tag "courses/4.png" %>
因此,您應該執行以下操作之一:
@course.image = "courses/n.png" # where n is any name for the image for that course
# then:
<%= image_tag @course.image %>
@course.image
中,但像您已經在做的那樣使用路徑: @course.image = n
<%= image_tag "course/#{@course.image}.png" %>
第一個當然是標准方式。 你的@course.image
應該有完整的路徑。 image_tag
需要一個字符串作為路徑,你的@course.image
應該有它,而不是你自己寫它的一部分。 請記住,文件擴展名是路徑的“一部分”,因此您不應忽略它。
提示:您也可以在此標簽中添加圖像的大小。 這非常有幫助:
<%= image_tag @course.image, size: "100x100" %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.