Skip to content

Commit bd0393b

Browse files
committed
Update README [ci skip]
1 parent 9b2555a commit bd0393b

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def data
225225
last_name: record.last_name,
226226
email: record.email,
227227
bio: record.bio,
228-
DT_RowId: record.id, # This will set the id attribute on the corresponding <tr> in the datatable
228+
DT_RowId: record.id, # This will automagically set the id attribute on the corresponding <tr> in the datatable
229229
}
230230
end
231231
end
@@ -398,6 +398,8 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
398398
end
399399
```
400400

401+
### Using view decorators
402+
401403
If you want to keep things tidy in the data mapping method, you could use
402404
[Draper](https://github.com/drapergem/draper) to define column mappings like below.
403405

@@ -640,7 +642,9 @@ In the end, it's up to the developer which model(s), scope(s), relationship(s)
640642
(or else) to employ inside the datatable class to retrieve records from the
641643
database.
642644

643-
### Creating indices for Postgresql
645+
## Pro Tips
646+
647+
### Create indices for Postgresql
644648

645649
In order to speed up the `ILIKE` queries that are executed when using the default configuration, you might want to consider adding some indices.
646650
For postgresql, you are advised to use the [gin/gist index type](http://www.postgresql.org/docs/current/interactive/pgtrgm.html).
@@ -683,6 +687,71 @@ $ bundle install
683687

684688
That's all :) ([Automatically prefer Yajl or JSON backend over Yaml, if available](https://github.com/rails/rails/commit/63bb955a99eb46e257655c93dd64e86ebbf05651))
685689

690+
### Use HTTP `POST` method
691+
692+
Use HTTP `POST` method to avoid `414 Request-URI Too Large` error. See : [#278](https://github.com/jbox-web/ajax-datatables-rails/issues/278).
693+
694+
You can easily define a route concern in `config/routes.rb` and reuse it when you need it :
695+
696+
```ruby
697+
Rails.application.routes.draw do
698+
concern :with_datatable do
699+
post 'datatable', on: :collection
700+
end
701+
702+
resources :posts, concerns: [:with_datatable]
703+
resources :users, concerns: [:with_datatable]
704+
end
705+
```
706+
707+
then in your controllers :
708+
709+
```ruby
710+
# PostsController
711+
def index
712+
end
713+
714+
def datatable
715+
render json: PostDatatable.new(view_context)
716+
end
717+
718+
# UsersController
719+
def index
720+
end
721+
722+
def datatable
723+
render json: UserDatatable.new(view_context)
724+
end
725+
```
726+
727+
then in your views :
728+
729+
```html
730+
# posts/index.html.erb
731+
<table id="posts-datatable" data-source="<%= datatable_posts_path(format: :json) %>">
732+
733+
# users/index.html.erb
734+
<table id="users-datatable" data-source="<%= datatable_users_path(format: :json) %>">
735+
```
736+
737+
then in your Coffee/JS :
738+
739+
```coffee
740+
$ ->
741+
$('#posts-datatable').dataTable
742+
ajax:
743+
url: $('#posts-datatable').data('source')
744+
type: 'POST'
745+
# ...others options, see [here](#5-wire-up-the-javascript)
746+
747+
$ ->
748+
$('#users-datatable').dataTable
749+
ajax:
750+
url: $('#users-datatable').data('source')
751+
type: 'POST'
752+
# ...others options, see [here](#5-wire-up-the-javascript)
753+
```
754+
686755
## Tutorial
687756

688757
You'll find a sample project [here](https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to). Its real world example.

0 commit comments

Comments
 (0)