Rails image_tag full path - ruby-on-rails

I have images in my assets folder and path to it
"app/assets/images/sets/img_2168.jpg"
so i want to use it like
Dir.glob("app/assets/images/sets/*.jpg") it return me "app/assets/images/sets/img_2168.jpg"
image_tag("app/assets/images/sets/img_2168.jpg") but it gives me empty image
Is it possible if so how to fix it?

Try the followings:
<%= image_tag("sets/" + File.basename(Dir.glob("app/assets/images/sets/*").first))%>
If you just provide "sets/img_2168.jpg" to image_tag, it should work.
You don't need to provide the complete path.

Try this one :
image_tag(File.read("#{Rails.root.to_s + '/assets/images/sets/img_2168.jpg'}").url)
OR
<img src="/assets/images/sets/img_2168.jpg"></img>
Ideally you should pass image object from the controller and in image_tag just get its url like :
image_tag(#image_obj.url)

The easiest way that I have found is to do this: The asset_url tag will put the full url of the asset (in this case an image called: "app-store.png") and then it will be passed to the image_tag normally.
<%= image_tag "#{ asset_url "app-store.png"}" %>

Rails 5 :
<%= image_tag(image_url('sets/img_2168.jpg')) %>
It work for me.

Related

Rails image_path/url helper methods

I have an image in app/assets/images/portfolio/01-example.jpg
I'm not entirely sure on how to use the image_url or image_path helpers to display this image in the view.
Is it <%= image_path('01-example.jpg') %>? If anyone could correct me with the correct syntax, I'd appreciate it!
Do you want to display it in an image tag, ie is your view html?
Then it's
<%= image_tag('portfolio/01-example.jpg') %>

Rails upload image can't be accessed

I upload an image to the Rails path\public\upload\member\1.jpg
but now I can't access this image.
No route matches [GET] "/public/upload/member/20131030203910.jpg"
So,what should I do?
<%= image_tag "/upload/member/1.jpg" %> try that and also check is it really upload folder or maybe uploads
better you can access the image using object
<%= object.image_url %>

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

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" >
How can I achive this? (Im using 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 :
#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")
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 %>
To ensure the operation, you can use:
#images = Dir.glob("#{Rails.root}/app/assets/images/camisas/*.jpg")
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.
files = Dir.glob('app/assets/images/slide/*')
files.each do |file|
puts file
end
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.

Is there a better way to display the filename in carrierwave?

At the moment
<%= link_to comment.file, comment.file_url %>
displays
/uploads/comment/file/6/IP___Addresses
Is there such thing as something like comment.file.filename ?
Is there a way to get the filename and display a link to that, so it would just say IPAddresses.txt and links to "/uploads/comment/file/6/IPAddresses" ?
Edit:
Figured it out
<%= link_to File.basename(comment.file.url), comment.file_url %>
You could have used the *_identifier method, in your case:
comment.file_identifier

Add image to layout in ruby on rails

I would like to add an image in my template for my ruby on rails project where i currenly have the code <img src="../../../public/images/rss.jpg" alt="rss feed" /> in a the layout stores.html.erb file however this doesn't seem to load as it looks like its missing a route which i'm not sure what its supposed to be.
Any ideas please?
Anything in the public folder is accessible at the root path (/) so change your img tag to read:
<img src="/images/rss.jpg" alt="rss feed" />
If you wanted to use a rails tag, use this:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
In a Ruby on Rails project by default the root of the HTML source for the server is the public directory. So your link would be:
<img src="images/rss.jpg" alt="rss feed" />
But it is best practice in a Rails project to use the built in helper:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
That will create the correct image link plus if you ever add assert servers, etc it will work with those.
When using the new ruby, the image folder will go to asset folder on folder app
after placing your images in image folder, use
<%=image_tag("example_image.png", alt: "Example Image")%>
simple just use the img tag helper. Rails knows to look in the images folder in the asset pipeline, you can use it like this
<%= image_tag "image.jpg" %>
It's working for me:
<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>
image_tag is the best way to do the job friend

Resources