Skip to content

Commit 344d09d

Browse files
committed
Merge pull request #1472 from edwinlunando/master
[DOC] update JSON adapter pagination links how to guide
2 parents b45f7b4 + 2678896 commit 344d09d

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

docs/howto/add_pagination_links.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,30 @@ ActiveModelSerializers pagination relies on a paginated collection with the meth
7474

7575
If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
7676

77-
In your action specify a custom serializer.
77+
Add this method to your base API controller.
78+
7879
```ruby
79-
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
80+
def pagination_dict(object)
81+
{
82+
current_page: object.current_page,
83+
next_page: object.next_page,
84+
prev_page: object.prev_page,
85+
total_pages: object.total_pages,
86+
total_count: object.total_count
87+
}
88+
end
8089
```
8190

82-
And then, you could do something like the following class.
91+
Then, use it on your render method.
92+
8393
```ruby
84-
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
85-
def initialize(object, options={})
86-
meta_key = options[:meta_key] || :meta
87-
options[meta_key] ||= {}
88-
options[meta_key] = {
89-
current_page: object.current_page,
90-
next_page: object.next_page,
91-
prev_page: object.prev_page,
92-
total_pages: object.total_pages,
93-
total_count: object.total_count
94-
}
95-
super(object, options)
96-
end
97-
end
94+
render json: posts, meta: pagination_dict(posts)
9895
```
96+
9997
ex.
10098
```json
10199
{
102-
"articles": [
100+
"posts": [
103101
{
104102
"id": 2,
105103
"title": "JSON API paints my bikeshed!",

0 commit comments

Comments
 (0)