Skip to content

Commit e975b07

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

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
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

@@ -683,6 +685,71 @@ $ bundle install
683685

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

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

688755
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)