Rails 如何使用 image_tag 列出文件夹中的所有图像? [英] Rails How To List all Images in a Folder using the image_tag?

查看:45
本文介绍了Rails 如何使用 image_tag 列出文件夹中的所有图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取app/assets/images/slide"文件夹中的所有图像,并将它们与标签(按顺序)放在一起.所以,它看起来像这样:

Im trying to take all the images in the 'app/assets/images/slide' folder and put them withing tags (in order). So, it will look like this :

<img src="1.jpg" >
<img src="2.jpg" >
<img src="3.jpg" >

我怎样才能做到这一点?(我使用的是 Rails 3.2.9)

How can I achive this? (Im using Rails 3.2.9)

这是我尝试过的代码(感谢 Khaled).但它输出所有图像路径的纯文本列表.我需要显示图像:

Here's the code I tried (thanks to Khaled). But it outputs a plain text list of all image paths. I need the images to show :

@images = Dir.glob("app/assets/images/slide/*.jpg")

@images.each do |image|
    image_tag image.gsub("app/assets/images/", "")
end

推荐答案

在您的控制器操作中,获取所有图像路径.

In your controller action, get all images paths.

@images = Dir.glob("app/assets/images/slide/*.jpg")

然后在你看来(假设是haml)

Then in your view (assuming haml)

- @images.each do |image|
   = image_tag "slide/#{image.split('/').last}"

假设 erb

 <% @images.each do |image| %>
   <%= image_tag "slide/#{image.split('/').last}" %>
 <% end %>

这篇关于Rails 如何使用 image_tag 列出文件夹中的所有图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆