author avatar

syedsibtain

Fri Jul 12 2024

Fixing Image Rendering in Rails with Active Storage
How to fix the error PG::UndefinedTable: ERROR: relation "active_storage_attachments" does not exist

This error occurs because Active Storage in Rails relies on specific database tables (e.g., active_storage_attachments and active_storage_blobs) to store metadata about attached files. If these tables do not exist, Rails cannot store or retrieve the necessary metadata for file attachments, resulting in the mentioned error.

By following these steps, we ensure that the necessary Active Storage tables are created, allowing Rails to store and retrieve image metadata correctly.

Run Active Storage Installation:


rails active_storage:install


Migrate the Database:


rails db:migrate


Restart the Rails Server:


rails server


Then in views we can simply use the helper and render the image


<%= image_tag url_for(recipe.image), class: "w-full h-64 object-cover" %>


#rails #activestorage