pagination question

Highlighted
Shopify Partner
15 0 0

I have a novice question.  Firstly, I am new to Shopify and Ruby.  I have worked through Michael Hartl's RoR tutorial, and followed Kevin Hughes' video on creating an app.  OK, I have a simple app running now, showing 10 products (of ~350 sample products) in my DevStore.  As my first task I wanted to add pagination for this list.  Firstly I tried the 'will-paginate' gem and code from the tutorial, but it just broke the app and it would not install at all.  I looked everywhere, search everything related to ShopifyApi & pagination, but could not find a sample of code that would show how to do this.  I would really appreciate it if someone please help with me with an example of how to paginate a simple product list? Thanks! Greg

home_controller.rb
class HomeController < ShopifyApp::AuthenticatedController
  def index
    @products = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
  end
end


index.html.erb
<% content_for :javascript do %>
  <script type="text/javascript">
    ShopifyApp.ready(function(){
      ShopifyApp.Bar.initialize({
        title: "Home",
        icon: "<%= asset_path('favicon.ico') %>"
      });
    });
  </script>
<% end %>

<h2>Products</h2>

<ul>
  <% @products.each do |product| %>
    <li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
  <% end %>
</ul>

 

0 Likes
Highlighted

Have you seen this topic?

https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/paginate-api-results-113066

I'm a software engineer. I make things happen automatically.
0 Likes
Highlighted
Shopify Partner
15 0 0

Łukasz Wiktor, Thank you!   I'll take a look.

Greg

0 Likes
Highlighted
New Member
13 0 0

I was following this tutotial to implement pagination. https://khawa.tech/en/articles/develop-shopify-application-ruby-rails

It uses will_paginate gem. The difference was that I used @products.paginate, instead of orders and I wasn't storing Products in my database. I got this error: undefined method `will_paginate' for #<ActiveResource::Collection...

UPDATE: After some searching I found that adding: require 'will_paginate/collection' to the controller solves the issue. Here is the final solution that woked for me. Hope this helps:

 

class HomeController < ShopifyApp::AuthenticatedController
  require 'will_paginate/collection'
 
  def index
      @shop = ShopifyAPI::Shop.current()
      @products = ShopifyAPI::Product.find(:all)
      
      count = @products.count
      page_limit = 25
      @total_pages = (count.to_f / page_limit.to_f).ceil
      @page = (params[:page].presence || 1).to_i
      @previous_page = "/?page=#{ @page - 1 }&limit=#{page_limit}" if @page > 1
      @next_page = "/?page=#{ @page + 1 }&limit=#{page_limit}" if @page < @total_pages
      @products = @products.paginate(page: @page, per_page: page_limit)
  end
end

 

 

 

Founder @ Virthium.com
0 Likes
Highlighted
Shopify Partner
9 0 1

I encountered another problem doing pagination.

 

Gem info:

  • omniauth-shopify-oauth2 (1.2.0)
  • shopify-kaminari (1.1.0)
  • shopify_api (4.3.2 494b37a)
  • shopify_app (7.4.0)

Gem info:

  • omniauth-shopify-oauth2 (1.2.0)
  • shopify-kaminari (1.1.0)
  • shopify_api (4.3.2 494b37a)
  • shopify_app (7.4.0)

Request:

parameters = {fields: 'id,title,url,handle', page: params[:page] || 1, limit: 5}parameters = parameters.merge(title: params[:title]) if params[:title].present?

@products = ShopifyAPI::Product.find(:all, params: parameters)

Issue:

When requesting WITH filter for title result doesn't include total_count.

Thus pagination doesn't worked, doesn't show in view.

<%= paginate @items, :pagination_class => 'pagination-centered', params: {title: params[:title], page: params[:page] || 1} %>

 

0 Likes