total_pages return in pagination with rails and kaminari

℡╲_俬逩灬. 提交于 2019-12-20 04:24:06

问题


i want a pagination in my page, im using ruby and kaminari to this.

class	ServicesController < ApplicationController
	def index
		/@services = Service.order(name: :asc)/
		@organs = Admin::Organ.all
	
		@services = @services.order(created_at: :desc).paginate(page:params[:page], per_page: 3 )
	end

this code, my control try paginate pages 3 to 3. In views:

<%= paginate @organ.services %>

As i want call the services with relationship to each organ i insert que last code im my view.

The result are:

 undefined method `total_pages' for #<Mongoid::Criteria:0x007f18a8b1f338>

if you can help, grateful for the attention!


回答1:


Try changing your controller code like the following:

class ServicesController < ApplicationController
    def index
      @organs = Admin::Organ.all
      @services = Service.order(created_at: :desc).page(params[:page]).per(3)
    end
end



回答2:


try this

Old code

@services = @services.order(created_at: :desc).paginate(page:params[:page], per_page: 3 )

New code

@services = Service.order(created_at: :desc).paginate(page:params[:page], per_page: 3 )



回答3:


I had a similar issue, it was because the commontator gem was bringing in methods from will_paginate and overriding the base ActiveRecord class.

The error was in the call stack for the page_entries_info method which seems to be a common method name between both libraries.

To fix, you can explicitly reference the method with this:

So the view code:

<%= Kaminari::Helpers::HelperMethods.page_entries_info @events %>
<%= link_to "Next", path_to_next_page(@events) %>
<%= link_to "Prev", path_to_prev_page(@events) %>

and in an initializer (initializers/kaminari_config.rb)

module Kaminari
  module Helpers
    module HelperMethods
      extend ActionView::Helpers::TranslationHelper
      module_function :page_entries_info
    end
  end
end


来源:https://stackoverflow.com/questions/36292808/total-pages-return-in-pagination-with-rails-and-kaminari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!