@@ -74,32 +74,30 @@ ActiveModelSerializers pagination relies on a paginated collection with the meth
74
74
75
75
If you are using ` JSON ` adapter, pagination links will not be included automatically, but it is possible to do so using ` meta ` key.
76
76
77
- In your action specify a custom serializer.
77
+ Add this method to your base API controller.
78
+
78
79
``` 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
80
89
```
81
90
82
- And then, you could do something like the following class.
91
+ Then, use it on your render method.
92
+
83
93
``` 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)
98
95
```
96
+
99
97
ex.
100
98
``` json
101
99
{
102
- "articles " : [
100
+ "posts " : [
103
101
{
104
102
"id" : 2 ,
105
103
"title" : " JSON API paints my bikeshed!" ,
0 commit comments