Rails如何使用image_tag列出文件夹中的所有图像?

时间:2020-11-30 00:27:42

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 :

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

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

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

我怎么能得到这个? (我使用Rails 3.2.9)

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 :

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

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

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

5 个解决方案

#1


21  

In your controller action, get all images paths.

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

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

Then in your view (assuming haml)

然后在你的视图中(假设haml)

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

Assuming erb

假设erb

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

#2


2  

To ensure the operation, you can use:

为确保操作,您可以使用:

@images = Dir.glob("#{Rails.root}/app/assets/images/camisas/*.jpg")

#3


1  

Works for me and displays images:

适合我并显示图像:

Dir.glob('app/assets/images/slide/*').map do |path| 
  image_tag "slide/#{ File.basename(path) }"
end.reduce(&:+)

You want to get rid of the full path with File#basename so it works with precompiled assets afaik. You also don't need to specify .jpg because its an images folder, and could have .png's.

您希望摆脱File#basename的完整路径,以便它可以与预编译的资产一起使用。您也不需要指定.jpg,因为它是一个images文件夹,并且可能有.png。

#4


0  

files = Dir.glob('app/assets/images/slide/*')
files.each do |file|
  puts file
end

#5


0  

From the documentation, image_tag returns an html image tag for the source. It cannot show all images at once. You should make a custom helper to read and walk through your directory.

从文档中,image_tag返回源的html图像标记。它无法一次显示所有图像。您应该创建一个自定义帮助程序来读取和遍历您的目录。

#1


21  

In your controller action, get all images paths.

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

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

Then in your view (assuming haml)

然后在你的视图中(假设haml)

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

Assuming erb

假设erb

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

#2


2  

To ensure the operation, you can use:

为确保操作,您可以使用:

@images = Dir.glob("#{Rails.root}/app/assets/images/camisas/*.jpg")

#3


1  

Works for me and displays images:

适合我并显示图像:

Dir.glob('app/assets/images/slide/*').map do |path| 
  image_tag "slide/#{ File.basename(path) }"
end.reduce(&:+)

You want to get rid of the full path with File#basename so it works with precompiled assets afaik. You also don't need to specify .jpg because its an images folder, and could have .png's.

您希望摆脱File#basename的完整路径,以便它可以与预编译的资产一起使用。您也不需要指定.jpg,因为它是一个images文件夹,并且可能有.png。

#4


0  

files = Dir.glob('app/assets/images/slide/*')
files.each do |file|
  puts file
end

#5


0  

From the documentation, image_tag returns an html image tag for the source. It cannot show all images at once. You should make a custom helper to read and walk through your directory.

从文档中,image_tag返回源的html图像标记。它无法一次显示所有图像。您应该创建一个自定义帮助程序来读取和遍历您的目录。