DatatablesServerSide
Simple rails gem to simplify the creation of datatables json response.
Installation
Add this line to your application's Gemfile:
gem 'datatables_server_side'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install datatables_server_side
Usage
This gem is inspired on Rails Casts #340 DataTables. Its recommended to watch.
Installation: run the installer generator
rails g datatables_server_side:installer
It will create a datatables folder inside the app folder
After that you will have to create a datatable.
To Generate a datatable run:
rails g dadatatables_server_side:datatable Example
this will create a ruby file /app/datatables/example_datatable.rb
You will have to define two methods:
A data method with the object that will be returned. example:
def data
records.map do |example|
[
example.id,
example.name,
example.description,
]
end
and a fetch_records method, that will be responsible for retrieve the query results from the model example:
def fetch_records
filter_params={}
filter_params[:offset]= params['iDisplayStart'] if params['iDisplayStart'].present?
filter_params[:limit]= params['iDisplayLength'].to_i if params['iDisplayLength'].present?
filter_params[:sort]= params['sSortDir_0'] if params['sSortDir_0'].present?
Example.find(:all,:params => filter_params)
end
finally you should add the the datatables object to the json response on your controller. example:
def index
respond_to do |format|
format.html
format.json{ render json: RodaDatatable.new(view_context)}
end
end
Contacts
Any questions contact me:
Website:http://layonferreira.com/
Email: [email protected]
License
MIT License
Copyright (c) 2015 Layon Ferreira