Skip to content

Pagination in Ruby on Rails

eWizardII edited this page Feb 19, 2012 · 2 revisions

This may be of help to people who aren’t intending to use other gems with indextank for pagination:

Basically do the following:

In your controller have something like this:

class MoviesController < ApplicationController
def index
 @movies = Movie.search(params[:search], '0') if params[:search].present?
end

and in the model have something like this:

# Movie Model
require 'open-uri'
require 'indextank'

api = IndexTank::Client.new(ENV['SEARCHIFY_API_URL'] ||'foo')
index = api.indexes('idx')

class Movie < ActiveRecord::Base
	# Obtain an IndexTank client
	def self.index
		@api = IndexTank::Client.new(ENV['SEARCHIFY_API_URL'] ||'foo')
		@index ||= @api.indexes('idx')
		@index
	end

	# IndexTank Retrival
	def self.search(search,start)
		index.search(search, :fetch => 'text,docid', :start => start)
	end
end

Now you will be allowed to pass on the url stuff like &start=10 or whatever you need for your pagination, this should also work for controlling things like len, etc.

Clone this wiki locally