Proper rails image_tag path - ruby-on-rails

I am trying to put an image on a mailer view using <%= image_tag("logo.png", :alt => "logo") %>. This works using an online link so I think the problem is figuring out the path. When the email is sent, the image does not appear and only the text 'logo' is showing.
I have the logo image at project_name > app > assets > images > logo.png.
I also have a public directory structured project_name > public > assets > image_and_js_files
What is the proper path I should use?

you can use
<%= image_tag("/assets/logo.png", :alt => "logo")

You have to specify the absolute link in your email in order to get it works. image_tag will only generate relative path, ex: /assets/logo.png.
Ex: https://stackoverflow.com/a/24561439/461640

Related

How to display image in rails with ActiveStorage

Rails: 6.1
Following the basic steps in the documentation of ActiveStorage I have added a avatar field for my User model and upload seems to be successful. But when I want to display #user.avatar I get a url for the image but the url seems to be 404. The image is not displayed.
Here is my image tag:
<img src="<%= image_path(url_for(#user.avatar)) %>" alt="Avatar for <%= #user.username %>">
<%= image_tag #user.avatar, class: 'user-image', alt: "Avatar for #{#user.username}" %>
Both of these are failing. Rendered HTML is here:
<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--569f9d466f5dd0d4917db040f0f87d948c0af667/paddle.png?locale=en" alt="Avatar for Someone">
Looking at the path, paddle.png is the image I uploaded and I assume upload is okay. But why doesn't it display?
Try this:
<%= image_tag #user.avatar.to_s, class: "user-image" %>
Okay, finally I found what was wrong. First thing ActiveStorage is not compatible with UUID models. What's more for a given 12hefhs-342jfsoeif-senfs-senfks UUID it takes the first consecutive digits and uses it as ID. Which is in my opinion a big issue. We don't get any error message like we do for other rails models when we try to save UUID in bigint (default id) field. To overcome this problem I had to add these migrations in this gist
Then the other problem was that I have a regex to redirect all requests to locale version of that same path. Like this:
get "/*path",
to: redirect("/#{I18n.default_locale}/%{path}", status: 302),
constraints: { path: /(?!(#{I18n.available_locales.join("|")})\/).*/ },
format: false
and this was redirecting image urls as well...
Removing this solves the problem. But introduces another one... How to redirect...

Rails image_tag full path

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.

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.

wicked_pdf image rendering

how do I get images of a product to show in pdf?
I have this code in view file
<div id="img-slide">
<% for asset in #car.assets %>
<%= image_tag(asset.asset.url(:medium)) %>
<% end %>
</div>
and it shows all images for this car.
but if I use the same code in show.pdf.erb then instead of images I got only question marks.. like the image missing thing.
So, is there a way to get them on paper? Thanks.
p.s. there is what console is showing
***************WICKED***************
Asset Load (0.2ms) SELECT `assets`.* FROM `assets` WHERE (`assets`.car_id = 29)
Carmodel Load (0.2ms) SELECT `carmodels`.* FROM `carmodels` WHERE `carmodels`.`id` = 28 LIMIT 1
Rendered cars/show.pdf.erb (255.2ms)
"***************/usr/bin/wkhtmltopdf -q - - ***************"
update
<%= pdf_image_tag('/public/system/assets/163/medium/2011_lincoln_navigator_l_angularfront.jpg', :style=>"margin:0px;padding:0px", :width=>"300", :height=>"240")%>
this code shows how the link should look like in html, with this code I can render one photo of the car, but it will be the same for all cars, so I didn't do much.
the 163 number is the id of assets that is assigned to car, here I keep one image with more sizes(thumb, medium, large..) and I got 5 maps with different numbers for one car. So I have lots of maps with numberes like this as I have at least 5 photos for each car. each car have 5 assets. In show.html I can see them, but not in pdf. I did put this in application helper:
def pdf_image_tag(image, options = {})
options[:src] = File.expand_path(RAILS_ROOT) + '' + image
tag(:img, options)
end
but this is only for images that you have on your server and will be the same for all cars, how can I get at least one image of each car to show in pdf? Pleaseeeee. help!!!
Version 0.7.9 is
<%= wicked_pdf_image_tag 'myfile.jpg' %>
It basically returns the absolute path on the file system:
file:///path/to/image/myfile.jpg
Also see: https://github.com/mileszs/wicked_pdf
Instead of using wicked_pdf_image_tag helper it's possible to use image_tag and image_url rails helpers together to get absolute path to image (this is what required for wicked_pdf gem):
image_tag image_url('dir/image.png')
here dir/image.png is image path relative to standard location in rails app (app/assets/images for images).
ok, so I found the answer
the <%= image_tag(asset.asset.url(:medium)) %> code generates the url to the image that in html will look like <img alt="2012_bmw_7_series_gearshift" src="/system/assets/174/original/2012_bmw_7_series_gearshift.jpg?1318267462"> so my images starts in system map and all I had to do is to write a method in application_helper.rb like this:
module ApplicationHelper
def pdf_image_tag(image, options = {})
options[:src] = File.expand_path(RAILS_ROOT) + '/public' + image
tag(:img, options)
end
end
this way wiked_pdf will know that image tag called pdf_image_tag will start from system folder in public and the final code for the pdf.html.erb will be:
<% for asset in #car.assets %>
<%= pdf_image_tag(asset.asset.url(:medium), :style=>"margin:0px;padding:0px", :width=>"250", :height=>"200")%>
<% end %>
was easy, but still took me a few days to figure it out. Nice day.
The only thing that worked for me is to use this:
<%= image_tag wicked_pdf_asset_base64('logo') %>
While the answer of #tap349 worked a bit for me, I had to do something completely different for images already stored in the project.
So, for images uploaded by the users, I use the mini_magick gem and in my views I have (I use slime syntax):
= image_tag image_url(picture.url(:medium))
For images already stored in the project I had to manually create the absolute path, by returning the image location and joining it with the request protocol and host like this (I use Webpacker and my images are in app/javascript/images):
= wicked_pdf_image_tag URI.join(request.base_url, asset_pack_path('media/images/logo.png'))
In another post I saw this command that helped me to see if the path existed or not, you can try it in the ruby console:
Webpacker.manifest.send(:data).keys.grep /logo/

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