I'm using rails 6.1.4 and ruby 3.1.1
My app is on Heroku. I have several images in my assets/images directory, but I cannot get some of them to show when I deploy to live. Other images in the app show, so I'm betting the ranch it's a code thing on my part. All images show on my local machine.
The app matches an image in assets/images/badges/ with an image_name in the Badge model. Currently there is NOT an image in assets/images/badges/ for every Badge.image_name.
A helper gets the images that match the image_names and send the names to the view in an array. But, if an image does not exist, I do not want to show a broken-image icon. So, I use if Rails.application.assets.find_asset(image_path) != nil to test for the image's existence, before adding it to the array. My guess is this is the problem area and I don't know how else to test for an image being in the assets/images/badges directory.
Code:
app/images/badges/ (just listing 3 here)
boxing-1-bronze-150.png
boxing-1-gold-150.png
boxing-1-silver-150.png
...
I've logged into heroku and can list the app/images/badges directory and I see these images are there and are named correctly.
badges_helprer.rb
badge_images = []
if badges
badges.uniq!
badges.each do |b|
image_path = "badges/#{b.image_name}"
if Rails.application.assets.find_asset(image_path) != nil
badge_images << image_path
end
end
end
return badge_images
View:
students/show
<% badges = get_badges_for_student(@student) %>
<% if badges %>
<% b_count = 0 %>
<% badges.each do |b| %>
<% if b_count == 3 %> <!-- 3 images per row -->
<br />
<% b_count = 0 %>
<% end %>
<%= image_tag(b) %>
<% b_count += 1 %>
<% end %>
<% end %>
As a test on my show view, I created an image tag for one of the images I know is there.
<p>Test: <%= image_tag("badges/boxing-1-gold-150.png") %></p>
It shows, so I go back to the helper page and am betting the line: if Rails.application.assets.find_asset(image_path) != nil is not working the way I think it should.
Is there a better way to test for an image file in the assets/images/badges/ directory.
Thanks for any help!
I found that this works:
Note:
Rails.rooton Heroku showsapp-name/appso the aboveRails.root.join('app','assets','images','badges',image_name)displaysapp/app/assets...when I print it out in a view. odd!! But I guess it knows how to navigate to the correct path.