Question

I want to override Kaminari's pagination when rendering JSON, or tell it to return all with pagination.

In App1, I am using ActiveResource to access App2's Group model:

class Group < ActiveResource::Base
  self.site = "http://www.app2.com:3000"
end

Here's App2's Group model:

class Group < ActiveRecord::Base
  default_scope order('name asc')  
  paginates_per 10

This is my controller. The Group.search stuff is ransack:

class GroupsController < ApplicationController
  # GET /groups
  # GET /groups.json
  def index
    @search = Group.search(params[:q])
    @groups = @search.result.page params[:page]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @groups }
    end
  end

I've added eleven groups to App2. In the console of App1 I get:

[45] pry(main)> Group.all.count
=> 10

What is the best way to do this without changing the HTML pagination rendering?

Était-ce utile?

La solution

You can prepare all the common logic you need but only apply pagination for the HTML format:

def index
  @search = Group.search(params[:q])
  @groups = @search.result

  respond_to do |format|
    format.html { @groups = @groups.page(params[:page]) }
    format.json { render :json => @groups }
  end
end

Autres conseils

You can run different code in different formats:

  def index
    respond_to do |format|
      format.html {
        @search = Group.search(params[:q])
        @groups = @search.result.page params[:page]
      } # index.html.erb
      format.json { 
        render json: Group.all
      }
    end
  end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top