nil:NilClass的未定义方法`total_pages'

时间:2019-11-08 18:56:35

标签: ruby-on-rails ruby kaminari

运行分页代码时出现此错误:

undefined method `total_pages' for nil:NilClass

我听不懂,也许你可以帮我吗?

products_controller.rb

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

帕吉纳:

<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

1 个答案:

答案 0 :(得分:0)

当我使用will_paginate gem时,我猜您正在使用

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

此外,我看到您的视图会迭代 avaliacao.questionario_respostas.approved.rated_questions,但您正在分页@products。当您显示每行是产品的表且希望对其进行分页时,gem起作用,因此您不会在同一视图中全部显示它们。

我希望这会有所帮助。