Angela Inniss
Angela Inniss

Reputation: 359

Image not showing for multiple image upload with active storage

I have followed a tutorial on how to upload multiple images using active record. I have installed active record, added my associations to my model and when I create a Service (my model) everything seems to be working when I upload 3 images i.e no Active Record errors however I cannot see my images on the FE, all I see is a broken images.

When I go into Rails c and type Service.images.last I can see that images is nil. Does anyone know why this is?

Is there anything else you need to know or see from me?

Thank you

My code to show the image:

<%[email protected] do  |img| %>
  <%= cl_image_tag @service.images, crop: :fill, class: 'card-image', class: 'card-image-show'%>
 <%end %>

Rails c

  Service Load (59.6ms)  SELECT  "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007fffc3ff08a0 @name="images", @record=#<Service id: 14, name: "Test Carpet cleaning", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 12:32:35", updated_at: "2019-08-19 12:32:35", photo: nil, images: nil>, @dependent=:purge_later>

Rails c when i do Service.last.images

irb(main):013:0> Service.last.images
  Service Load (0.6ms)  SELECT  "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007f92fd1f8780 @name="images", @record=#<Service id: 15, name: "test multiple images", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 18:56:12", updated_at: "2019-08-19 18:56:13", photo: nil, images: nil>, @dependent=:purge_later>

Service model

class Service < ApplicationRecord
  has_many_attached :images
  mount_uploader :photo, PhotoUploader
end

I would like my 3 images to display on the FE

Upvotes: 0

Views: 942

Answers (1)

Angela Inniss
Angela Inniss

Reputation: 359

This is the code that I finally used to get the imageS to show on my SHOW PAGE.

<% if @service.images.attached? %>
<p>
  <strong>Images:</strong>
  <br>
  <% @service.images.each do |image| %>
    <%= image_tag(image) %>
  <% end %>
</p>
<% end %>

I was using cl_image_tag to display my image which was causing the image to display broken. I should have used the above.

Source:https://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond

Upvotes: 0

Related Questions