Skip to content

Instantly share code, notes, and snippets.

@Andrew8xx8
Created June 14, 2013 10:32
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Andrew8xx8/5780902 to your computer and use it in GitHub Desktop.
Save Andrew8xx8/5780902 to your computer and use it in GitHub Desktop.
Example of API with ransack search and kaminari paginate.
class Api::CitiesController < Api::ApplicationController
respond_to :json
##
# Get list of cities.
#
# @note Default per_page option is 10.
#
# @overload index(q)
# @param [Hash] q Ransack search paramsh
#
# @see https://github.com/ernie/ransack/wiki/Basic-Searching
def index
q_param = params[:q]
page = params[:page]
per_page = params[:per_page]
@q = City.ransack q_param
@cities = @q.result.page(page).per(per_page)
end
##
# Get city by ID
#
def show
@city = City.find_by_sub_domain params[:id]
end
end
class City < ActiveRecord::Base
attr_accessible :name, :location
validates :name, :presence => true, :uniqueness => true
end
gem 'kaminari'
gem 'ransack'
# In config/initializers
Kaminari.configure do |config|
config.default_per_page = 10
config.window = 2
config.param_name = 'q[page]'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment