total_pages return in pagination with rails and ka

2019-03-02 09:37发布

问题:

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 )