Ask A Question

Notifications

You’re not receiving notifications from this thread.

Having a problem to Paginate an iteration with Kaminari

Houssein abil asked in Rails

I'm trying to paginate a result of an iteration using Kaminari. The pagination is showing well but its not cutting it into another page. All is showing on the same page even when I click on page 2, it still the same result as page 1

I'm using rails 6 and ruby 2.5

in my controller i have this

def dashboard
    user_gigs = current_user.gigs
    @user_gigs_pager = Kaminari.paginate_array(user_gigs).page(params[:page]).per(4)
  end

then in my view I call the paginate

<div class="card">
   <div class="card-header-title is-centered">
     <%= paginate @user_gigs_pager %>
   </div>
</div>

this my full view code

 <% current_user.gigs.each do |gig| %>
                  <%# <% user_gigs = current_user.gigs  %>
                    <div class="column is-one-third">
                        <div class="card">
                            <div class="card-image">
                              <%= link_to edit_gig_path(gig) do %>
                                <figure class="image is-4by3">
                                    <!-- Gig Cover Image Here -->
                                    <%= image_tag gig_cover(gig) %>
                                </figure>
                              <% end %>
                            </div>

                            <div class="card-content p-t-5 p-b-5">
                                <p class="subtitle is-6 m-b-5"><%= link_to gig.title.truncate(20), gig_path(gig) %></p>
                                <span class="star-review"><i class="fa fa-star"></i>
                                  <%= gig.average_rating %> <span class="star-review">(<%= gig.reviews.count %>)</span>
                                </span>
                            </div>

                            <footer class="card-footer">
                              <p class="deletegig">
                                <%= link_to destroy_gig_path(gig), method: :delete, data: { confirm: "are you sure"} do  %>
                                  <span class="icon has-text-danger">
                                    <i class="far fa-trash-alt"></i>
                                  </span>
                                <% end %>
                              </p>
                               <% basic_price = gig.pricings.find{ |p| p.pricing_type == 'basic' }%>
                                <a class="has-text-danger is-block card-footer-item has-text-right">
                                  <% if !basic_price.nil? %>
                                    <span class="small-title">Points</span> <i class="fab fa-bitcoin"></i>
                                    <strong><%=  basic_price.price %></strong>
                                  <% else %>
                                    <strong>
                                      <span class="small-title has-text-dark">Pas de point</span>
                                    </strong>
                                  <% end %>
                                </a>
                            </footer>

                        </div>
                    </div>

                  <% end %>
                </div>
            </div>
        </div>
          <div class="card">
              <div class="card-header-title is-centered">
                <%= paginate @user_gigs_pager %>
              </div>
          </div>
Reply

You should try just paginating on the ActiveRecord association instead of the array.

def dashboard
    @gigs = current_user.gigs.page(params[:page]).per(4)
  end

Then in the view

<%= @gigs.each do |gig| %>
<% end %>

<%= paginate @gigs %>
Reply

Works like a charm. Thanks @chris Oliver. You are our hero. I have been strugling with this since Saturday

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.