Introducing gem (Kaminari) recommended for those who want to organize images lined up in a row

Hello,

I am aspiring to be a freeter blogger engineer. (Naga)


This time, it's one of the gems that really shocked me when I actually used it.

kaminari

I would like to introduce you to a Gem with a cool name! !!

Do you think that it becomes difficult to see when product images are accumulated while developing a post-type application?

It's a Gem that solves such problems, so if you are making a post-type app, please use it! !!


First of all, as a premise This time, I will talk about the introduction to the existing application.

So I haven't made a posting function yet ~ Please start from there.

So first open the Gemfile

gem 'kaminari'

And write to the bottom

bundle install

Let's do it

Next, the view file to which you want to apply pagination, In the controller file Add entry In my case, I wanted to pagination the book, so I wrote

books_controller.rb



  def index
   @books =Book.all
   @books = Book.page(params[:page]).per(1).order('updated_at DESC')
   @book =Book.includes(:user)
   set_book_column 
  end

book.html.erb


<%#Book information%>
  <div>
    <% @books.each do |book|%>
     <% if book.present?%>
      <tr>
        <td> <h3 class="item-name"><%= book.genre.type %></h3></td>
        <td> <h2 class="item-name"><%= book.name %></h2></td>
        <%= link_to "/books/#{book.id}" do %>
        <td><%= image_tag book.image , id: 'slideshow' if book.image.attached?%></td>
        <% end %>
      </tr>
     <%end%>
    <% end %>
   </div>
   <div id="page">
     <%=paginate @books %>
   </div>
    

The added description is

books_controller.rb


   @books = Book.page(params[:page]).per(1).order('updated_at DESC')

index.html.erb


<%=paginate @books %>

No way this Only: sunny:

gem is amazing lol (vocabulary)

By the way

.order('updated_at DESC')

If you add, you can change the order from the latest one If you want to stick to it, let's put it in.

Also, for those who want to improve the design

application.html.erb


<!DOCTYPE html>
<html>
  <head>
    <title>Books</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">Add a sentence! !!
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
  </head>

  <body>
    <%= yield %>
  </body>
</html>

after

rails g kaminari:views bootstrap3

Then you can make a decent design by default

Please use it! !!

Also, If there is something wrong We apologize for the inconvenience, but please comment so that the people who read this article will not be damaged! !!

This article was written with reference to the following information.

-[Rails beginner] Implement pagination and change the design to your liking -[Ruby on Rails] Add pagination function with gem (Kaminari) and apply Bootstrap.

Recommended Posts

Introducing gem (Kaminari) recommended for those who want to organize images lined up in a row
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
I put in a gem bullet (for those who don't notice the N + 1 problem themselves)
VS Code FAQ for those who want to escape Eclipse