'showing a default mage on bootstrap if images are not attached

I am in a bootstrap carousel, I want to call a method that will play the images in the carousel IF they are attached and that will play a default image (in the carousel) IF they are not attached.

Here is my method :

def bien_picture_carousel
    if images.attached?
      images.each do |image|
        image.key
      end
    else
      "bien-par_defaut_shvulu"
    end
  end

Here is my bootstrap carousel iteration (my carousel works perfectly well without the call of the method)

<% @bien.images.each.with_index do |image, i| %>
      <div class="carousel-item >
        <%= cl_image_tag @bien.bien_picture_carousel%>         **<--- error is on this line**
      </div>
  <% end %>

Issue : I have this error message : « no implicit conversion of ActiveStorage::Attachment into String »

I don’t really understand this error message. What can I do to make it works ?

EDIT :

I have updated my approach :

  <div class="carousel-inner">
    <% if @bien.images.attached? %>
      <% @bien.images.each.with_index do |image, i| %>
        <div class="carousel-item" >
          <%= cl_image_tag image.key%>
        </div>
        <% end %>
    <% else %>
       <% @bien.images.each.with_index do |image, i| %>
          <div class="carousel-item" >
            <%= cl_image_tag "bien-par_defaut_shvulu" %>
          </div>
          <% end %>
    <% end %>

The else part is not showing on screen



Solution 1:[1]

Solved,

a class active was missing.

I replaced

 <% else %>
          <div class="carousel-item" >
            <%= cl_image_tag "bien-par_defaut_shvulu" %>
          </div>
          <% end %>
    <% end %>

by

<% else %>
            <div class="carousel-item active" >
              <%= cl_image_tag "bien-par_defaut_shvulu" %>
            </div>
          <% end %>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1