You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Rails, a common pattern would be to simply paginate a
collection and then render it using a custom serializer. Although this
is currently possible by calling `_paginate_collection` directly, it was
unintended. To better support custom serialization or things like
JBuilder, allow users to bypass the implicit render at the end of
`paginate` by passing their collection to it directly instead of via the
`:json` or `:xml` options:
```ruby
class MoviesController
def index
movies = paginate(Movie.all, per_page: 25)
render json: MoviesSerializer.new(movies)
end
end
```
If the call to `paginate` still mimics what a call to `render` looks
like, an implicit render will happen. If it looks more like the above,
the collection will simply be paginated and returned after the headers
are set up.
Signed-off-by: David Celis <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+23-8Lines changed: 23 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ gem 'api-pagination'
25
25
26
26
## Rails
27
27
28
-
In your controller, provide a pageable collection to the `paginate` method:
28
+
In your controller, provide a pageable collection to the `paginate` method. In its most convenient form, `paginate` simply mimics `render`:
29
29
30
30
```ruby
31
31
classMoviesController < ApplicationController
@@ -47,18 +47,33 @@ class MoviesController < ApplicationController
47
47
end
48
48
```
49
49
50
-
`paginate` will:
50
+
This will pull your collection from the `json` or `xml` option, paginate it for you using `params[:page]` and `params[:per_page]`, render Link headers, and call `ActionController::Base#render` with whatever you passed to `paginate`. This should work well with [ActiveModel::Serializers](https://github.com/rails-api/active_model-serializers). However, if you need more control over what is done with your paginated collection, you can pass the collection directly to `paginate` instead of in a way that mimics `render`:
51
51
52
-
1. Pull your collection from `json:` or `xml:`
53
-
2. Use `params[:page]` and `params[:per_page]` to paginate your collection for you
54
-
3. Use the paginated collection to render `Link` headers
55
-
4. Call `ActionController::Base#render` with whatever you passed to `paginate`.
This will avoid implicitly calling `render` at the end. Instead, `paginate` will simply set up the headers and return your collection so you can do whatever you want with it.
56
71
57
-
The collection sent to `paginate`_must_ respond to your paginator's methods. For Kaminari, `Kaminari.paginate_array` will be called for you behind-the-scenes. For WillPaginate, you're out of luck unless you somewhere`require 'will_paginate/array'`. Because this pollutes `Array`, it won't be done for you automatically.
72
+
Note that the collection sent to `paginate`_must_ respond to your paginator's methods. For Kaminari, `Kaminari.paginate_array` will be called for you behind-the-scenes. For WillPaginate, you're out of luck unless you call`require 'will_paginate/array'` somewhere. Because this pollutes `Array`, it won't be done for you automatically.
58
73
59
74
## Grape
60
75
61
-
Grape is similar, though `paginate`won't take options. Only your collection. In your API endpoint:
76
+
With Grape, `paginate`is used to declare that your endpoint takes a `:page` and `:per_page` param. Inside your API endpoint, it simply takes your collection:
0 commit comments