htmlruby-on-railsblobruby-on-rails-7alt-attribute

image filename doesn't show the name of the image but it only shows numbers in rails7


When I write this code to show image filename for the alt :

<img class="card-img-top" alt="<%= store.main_image.filename %>" draggable="false" src="<%= my_store_url(store) %>" />

by using :

store.main_image.filename

for alt, it doesn't show the actual filename, but numbers like this 1000043.jpg in production environment.

But when I use it in development environment, it does show the actual filename.


Solution

  • Development:

    Production:

    <img class="card-img-top" alt="<%= store.main_image.blob.filename.to_s %>" draggable="false" src="<%= my_store_url(store) %>" />
    
    

    store.main_image.blob.filename.to_s ensures you get the actual filename stored in Active Storage.