简体   繁体   English

无法通过 image_tag 在 rails 中显示图像

[英]Can not show the image by image_tag in rails

I am trying to make a very simple rails program to show a picture and here is my show.html.erb file我正在尝试制作一个非常简单的 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>

What I want is, for example, in the folder courses, there are 4 images 1 2 3 4, when I connect to the http://localhost:3000/courses/2 , it will show the picture 2, but it gave me this problem The asset "courses/image" is not present in the asset pipeline.我想要的是,例如,在课程文件夹中,有 4 张图片 1 2 3 4,当我连接到http://localhost:3000/courses/2时,它会显示图片 2,但它给了我这个问题The asset "courses/image" is not present in the asset pipeline.

I am very confusing am tried to fixed it but could not.我很困惑我试图修复它但不能。 I am a newbie and still stydying, could you please give me some ideas?我是新手,还在stydying,你能给我一些想法吗? Thank you very much.非常感谢你。

What you need in different ways is:您需要的不同方式是:

  <%= image_tag "courses/1.png" %>
  <%= image_tag "courses/2.png" %>
  <%= image_tag "courses/3.png" %>
  <%= image_tag "courses/4.png" %>

So, you should do one of these:因此,您应该执行以下操作之一:

  • Try saving full path_name (after images directory) for every course ie尝试为每门课程保存完整路径名(在图像目录之后),即
  @course.image = "courses/n.png" # where n is any name for the image for that course
  # then:
  <%= image_tag @course.image %>
  • Just save the number in @course.image but use the path like you are already doing:只需将数字保存在@course.image中,但像您已经在做的那样使用路径:
  @course.image = n
  <%= image_tag "course/#{@course.image}.png" %>

First one is the standard way of course.第一个当然是标准方式。 Your @course.image should have the complete path in it.你的@course.image应该有完整的路径。 image_tag is expecting a string which will be a path and your @course.image should be having it, instead of you writing a part of it yourself. image_tag需要一个字符串作为路径,你的@course.image应该有它,而不是你自己写它的一部分。 Remember, that file extension is a "part" of the path so you should not ignore it.请记住,文件扩展名是路径的“一部分”,因此您不应忽略它。

Hint: You can add size of the image too in this tag.提示:您也可以在此标签中添加图像的大小。 It is very helpful:这非常有帮助:

  <%= image_tag @course.image, size: "100x100" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM