undefined method `each' for #<ImagesUploader::UploadedFile:0x00007f8a0f0c0760>

304 views Asked by At

In my view I have this:

<% @item.images.each do |image| %>
 <%= image_tag image.images_url(:large) %>
<% end %>

If I have one image uploaded, it returns the error undefined method 'each' for #<ImagesUploader::UploadedFile:0x00007f8a0f0c0760>, but if I upload more than one, this view works fine. Alternately, <%= image_tag @item.images_url(:large) %> without the enclosing block works just fine.

I did not have this problem prior to switching from ActiveStorage to Shrine.

How can I write my view so that it shows all uploaded images, whether it's one or many?

1

There are 1 answers

0
Salil On BEST ANSWER

You can do something like following

<% (Array @item.images).each do |image| %>

This will always return the array, check following examples.

2.5.1 :001 > Array 5
 => [5]
2.5.1 :002 > Array [5]
 => [5]
2.5.1 :003 > Array []
 => []