Skip to content

Instantly share code, notes, and snippets.

@jrhorn424
Forked from Andrew8xx8/Gemfile
Created April 11, 2014 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrhorn424/10478476 to your computer and use it in GitHub Desktop.
Save jrhorn424/10478476 to your computer and use it in GitHub Desktop.
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