문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top