Merge pull request #1472 from edwinlunando/master

[DOC] update JSON adapter pagination links how to guide
This commit is contained in:
Benjamin Fleischer 2016-02-12 14:13:40 -06:00
commit 344d09d36e

View File

@ -74,32 +74,30 @@ ActiveModelSerializers pagination relies on a paginated collection with the meth
If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key. If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
In your action specify a custom serializer. Add this method to your base API controller.
```ruby
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
```
And then, you could do something like the following class.
```ruby ```ruby
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer def pagination_dict(object)
def initialize(object, options={}) {
meta_key = options[:meta_key] || :meta
options[meta_key] ||= {}
options[meta_key] = {
current_page: object.current_page, current_page: object.current_page,
next_page: object.next_page, next_page: object.next_page,
prev_page: object.prev_page, prev_page: object.prev_page,
total_pages: object.total_pages, total_pages: object.total_pages,
total_count: object.total_count total_count: object.total_count
} }
super(object, options)
end
end end
``` ```
Then, use it on your render method.
```ruby
render json: posts, meta: pagination_dict(posts)
```
ex. ex.
```json ```json
{ {
"articles": [ "posts": [
{ {
"id": 2, "id": 2,
"title": "JSON API paints my bikeshed!", "title": "JSON API paints my bikeshed!",