[RUBY] How to create pagination for a "kaminari" array

Introduction

This time, I will introduce the sample code to create pagination for the array and display it in the view.

controller

I wrote a code like this on the controller to display my favorite products on the user's My Page. Actually, this can be done more easily by associating, so please refer to the following article! https://qiita.com/yummy888/items/22db2f8b79b5be148b69

 def index
    items = []
    likes = Like.where(user_id: params[:item_id])
    if likes.present?
      likes.each { |like| items << Item.find(like.item_id)}
    end
    @items = Kaminari.paginate_array(items).page(params[:page]).per(15)
  end

Why are you passing params [: item_id] to user_id? You may have thought, but since likes are nested in items in root.rb, this is how to pass parameters. (In the parameter, current_user is passed by path where it is: item_id) If it is true, you can nest items in uses and then nest likes to pass more beautiful parameters.

View

I'm doing it with haml. If it is erb, it feels like pagination is placed just below the end of each statement.


 - @items.each do |item|
   = link_to item_path(item), class: "user__liked__items" do
     .user__liked__items__image
       = image_tag "#{item.images[0].image.url}", height:"50px", width: "50px", class: "user__liked__item__image"
       -if item.buyer_id.present? 
         .items__image__sold
           .items__image__sold__inner
              SOLD
       .user__liked__item__details
         .user__liked__item__name
           = item.name
         .user__liked__item__price
           %span
             ¥
             = item.price.to_s(:delimited)
           %i.fas.fa-chevron-right
  = paginate @items

Summary

When I can implement various things by myself, I feel that I have abandoned the search for an easier implementation method, so I would like to improve that ... Kanno: santa:

But if you want to create pagination, do you have to do various things with the controller anyway? ?? You can't do params [: page] in the view, right? Perhaps

Would you like to try a different implementation method the next time you implement this feature: frowning2:

Reference article

https://blog.konboi.com/post/2013/03/31/224939/

Recommended Posts

How to create pagination for a "kaminari" array
How to create a Maven repository for 2020
How to create a database for H2 Database anywhere
How to create a method
[Rails] How to create a signed URL for CloudFront
How to use an array for a TreeMap key
[Spring Boot] How to create a project (for beginners)
[Java] How to create a folder
How to make a Java array
How to create a lightweight container image for Java apps
How to output array values without using a for statement
How to create and launch a Dockerfile for Payara Micro
How to add a new hash / array
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Rails] How to create a graph using lazy_high_charts
How to use an array for HashMap keys
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
How to make a lightweight JRE for distribution
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
[1st] How to create a Spring-MVC framework project
How to create docker-compose
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Java] [For beginners] How to insert elements directly in a 2D array
How to implement Pagination in GraphQL (for ruby)
How to make a judgment method to search for an arbitrary character in an array
How to create a Java environment in just 3 seconds
How to write a unit test for Spring Boot 2
How to create a JDBC URL (Oracle Database, Thin)
How to write to apply gem Pagy (pagination) to an array
How to create a data URI (base64) in Java
[For those who create portfolios] How to use font-awesome-rails
How to convert a file to a byte array in Java
How to make a mod for Slay the Spire
[Apple Subscription Offer] How to create a promotional offer signature
I want to create a generic annotation for a type
How to create an application
How to leave a comment
[Rails] How to use "kaminari"
How to initialize Java array
How to insert a video
Tutorial to create a blog with Rails for beginners Part 1
SDWebImage: How to clear the cache for a particular UIImageView
How to divide a two-dimensional array into four with ruby
How to create a form to select a date from the calendar
How to change a string in an array to a number in Ruby
How to create a placeholder part to use in the IN clause
[For Ruby beginners] Explain how to freely delete array elements!
[For those who create portfolios] How to use chart kick
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to test a private method with RSpec for yourself
How to add the same Indexes in a nested array
[For those who create portfolios] How to omit character strings
Minecraft Modding [1.12] How to attach a special render for Item
Tutorial to create a blog with Rails for beginners Part 2
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to create a service builder portlet in Liferay 7 / DXP
Tutorial to create a blog with Rails for beginners Part 0
How to add columns to a table