Rails How to List All Images in a Folder Using the Image_Tag

Rails How To List all Images in a Folder using the image_tag?

In your controller action, get all images paths.

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

Then in your view (assuming haml)

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

Assuming erb

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

Looping images from folder in to photo gallery

SOLVED

This code worked for me

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

Have Rails 'image_tag' to browse image outside the /public folder?

Assuming you can put the files somewhere more permanent than /tmp, storing and accessing uploaded files outside the public directory is possible using the :url and :path parameters, along with a bit of configuration work.

An example extracted from code I wrote recently (it's modified enough that it might not work if you copy it verbatim):

app/models/picture.rb
# Define the attachment
has_attached_file :image,
:styles => {:large => ["700", :jpg],
:thumb => ["100x100>", :gif]},
:url => "/asset/picture/:id/:style/:basename.:extension",
:path => ":base/picture/:id/:style/:basename.:extension"

config/initializers/storage.rb
# Configure common attachment storage
# This approach allows more flexibility in defining, and potentially moving,
# a common storage root for multiple models. If unneeded, just replace
# :base in the :path parameter with the actual path
Paperclip.interpolates :base do |attachment, style|
/path/to/persistent/storage
# A relative path from the Rails.root directory should work as well
end

app/controllers/pictures_controller.rb
# Make the attachment accessible
def asset
instance = Picture.find(params[:id])
params[:style].gsub!(/\.\./, '')
#check permissions before delivering asset?
send_file instance.image.path(params[:style].intern),
:type => instance.image_content_type,
:disposition => 'inline'
end

config/routes.rb
# Add the necessary route
match '/asset/picture/:id/:style/:basename.:extension', :to => 'pictures#asset'

app/views/pictures/show.html.erb
<% # Display the picture %>
<%= image_tag picture.image.url(:large) %>

Note this is all Rails 3 syntax. A handful of changes would be needed to use it in Rails 2.x, but hopefully you get the, um, picture.

Access public folder from image_tag in Rails view

If you are using Rails 3.2+, I would suggest you save images into your "app/assets/images" folder.

Then you can use something like this:

<%= image_tag "#{current_user.email}.png" %>

You can also use subdirectories:

<%= image_tag "generated_images/#{current_user.email}.png" %>

Learn more about the asset pipeline here:
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

How to display image from rails local project folder?

I was browsing and saw this SO post.
I was able to get it work using

<%= image_tag ("/assets/mockup/img2.jpg"), class: "img-responsive"%>

Image path was assets/images/mockup/img2.jpg; omitting images from the image path assets/mockup/img2.jpg solves the issue.



Related Topics



Leave a reply



Submit