简体   繁体   English

Rails迭代erb视图中的图像

[英]Rails iterate images in erb views

So I had an error earlier and finally got it working but not the way it's suppose to. 所以我早些时候遇到了一个错误,终于使它起作用了,但是没有达到预期的效果。 So users can create a post with pictures. 因此,用户可以创建带有图片的帖子。 When you go to show.html.erb all the pictures show up that belong to that post. 当您前往show.html.erb时,所有属于该帖子的图片都会显示出来。 When you go to index.html.erb to see all post created in the database, each post has a image, more like a thumbnail. 当您转到index.html.erb来查看数据库中创建的所有帖子时,每个帖子都有一个图像,更像是一个缩略图。 The code i have below shows the thumbnail image but that image doesnt belong to that post.For example, the post on the right in the picture is suppose to be a red car, and the post on the left is suppose to be a blue car but it's not. 我下面的代码显示了缩略图图像,但该图像不属于该帖子。例如,图片右侧的帖子假设是红色汽车,而左侧的帖子假设是蓝色汽车但事实并非如此。 The image on both post belongs to a whole different post. 两个帖子上的图像都属于一个完全不同的帖子。 enter image description here 在此处输入图片说明

                    <figure>
                      <div class="icon">
                        <i class="lni-heart"></i>
                      </div>
                      <% if post.image.attached? %>
                         <%= image_tag post.image, :class => 'img-fluid' %>
                      <% end %>
                    </figure>

post.image by itself is just the object. post.image本身就是对象。 You need to specify the name attribute of image or even the thumb 您需要指定图像甚至拇指的名称属性

<%= image_tag post.image.name %> 

or 要么

<%= image_tag post.image.name.thumb %>

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

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