Search code examples
ruby-on-railsrails-activestorageruby-on-rails-5.2

request active storage image show only one not all images


As Rookie Rails, i am able to get all images to display on the html.erb as using model as has_many_attached :images

but i have tried many different way like this code below

<div class="row">
  <div class="col-md-12">
      <% if @tool.images.attached? %>
            <% @tool.images.each do |image| %>
                <div class="img-fluid <%= 'active' if image.id == @images[0].id %>">
                  <%= link_to image_tag(image), image %>
                </div>
            <% end %>
        <% end %>
    </div>
</div>

but didn't show one image but only a few images as I am trying to fix this

image.id == @images[0].id

this code is dont display one image, do you know where i can use this code into this as i have tried many way but all failed

if i use this code

<!-- Image -->
<div class="row">
  <div class="col-md-12">
    <% if @tool.images.attached? %>
        <% @tool.images.each do |image| %>
            <%= link_to image_tag(image, class:"tools-gallery"), image %>
        <% end %>
    <% end %>
  </div>
</div>
<br/>

it show fine and able to get displayed

enter image description here

but I don't want two images or more, it ok for the gallery but not this pages as it need show one image.

and the controller.rb

  def show
    @images = @tool.images
  end

Solution

  • <div class="row">
      <div class="col-md-12">
        <%= image_tag @tool.images.first if @tool.images.attached? %>
      </div>
    </div>