[Ruby on Rails] Carousel of bootstrap4 is implemented as a slide show using each method.


After using each method Implement a slide show according to the number of registered images.

Development environment

ruby 2.5.7 Rails OS: macOS Catalina


[Ruby on Rails] Post image preview function in refile It is OK if the image posting function is available.

Regarding bootstrap4, this article was easy to understand. Use Bootstrap 4 with Rails app

Check the official website

Carousel --Bootstrap 4.1 Japanese Reference

This time in this Attempts to implement using the With indicators method.

edit view


<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="3000">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <% @post_images.drop(1).count.times do |index| %>
      <li data-target="#carouselExampleIndicators" data-slide-to="#{index}"></li>
    <% end %>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <% @post_images.first(1).each do |image| %>
        <%= attachment_image_tag image, :image, class: "carousel_image"  %>
      <% end %>
    <% @post_images.drop(1).each do |image| %>
      <div class="carousel-item">
        <%= attachment_image_tag image, :image, class: "carousel_image" %>
    <% end %>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>

Way of thinking

Since the image is written directly on the official website, in order to get closer to this goal The first one is specified as ```