1

I have this error when run my pagination code:

undefined method `total_pages' for nil:NilClass

I cant understand, maybe you can help me?

products_controller.rb

@products = Product.order(:nome).page params[:page]

página:

<ul class="product-evaluation-rating">
    <% avaliacao.questionario_respostas.approved.rated_questions.each do |answer| %>
    <li class="product-evaluation-rating-item">
    <%= answer.questionario_pergunta.custom_question %>
    <%= content_tag :p, class: (answer.questionario_pergunta.custom_question == 'recomendaria' ? 'strong' : nil) do %>
    <%= answer.custom_answer %>
    <% end %>
    </li>
    <% end%>
    </ul>
    <%= paginate @products %>

kaminari_config.rb:
    # frozen_string_literal: true
    Kaminari.configure do |config| 
    # config.default_per_page = 25
    # config.max_per_page = nil
    # config.window = 4
    # config.outer_window = 0
    # config.left = 0
    # config.right = 0
    # config.page_method_name = :page
    # config.param_name = :page
    # config.params_on_first_page = false
    end
Ursus
  • 29,643
  • 3
  • 33
  • 50
  • 1
    Maybe [this answer](https://stackoverflow.com/questions/1408852/will-paginate-undefined-method-total-pages) or [this answer](https://stackoverflow.com/questions/36070991/kaminari-undefined-method-total-pages) will be helpful. – calebkm Nov 08 '19 at 19:16
  • Whenever you get undefined method for nil, its because something is trying to call a method on nil. In this case my guess would be that the result of Product.order(:none).page params[:page] is nil so @products is nil, and somewhere in the paginate method there is a call to total_pages. – nPn Nov 09 '19 at 00:17

1 Answers1

0

When I use the will_paginate gem which I guess you are using I use

@products = Product.order(:nome).paginate(:page => params[:page])

Also, I see that your view iterates avaliacao.questionario_respostas.approved.rated_questions but you are paginating @products. The gem works when you show a table where each line is a Product and you want to paginate them so that you do not show them all in the same view.

I hope this helps.

Adolfo
  • 66
  • 5