Setup:
Rails 5, using ActiveModel::Serializer and Kaminari
Sample code:
def index
@catalogs = Catalog.page(params[:page])
render json: @catalogs, adapter: :json_api
end
Problem:
When params[:page]
is nil
, my result is as expected:
{
"data": [
{
"id": "a020ab21-9028-4bfd-8f9c-1b735ed4734b",
"type": "catalogs",
"attributes": {
"name": "First",
"locale": "en"
}
}
],
"links": {
"self": "http://localhost:3000/v1/catalogs?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"next": "http://localhost:3000/v1/catalogs?page%5Bnumber%5D=2&page%5Bsize%5D=1",
"last": "http://localhost:3000/v1/catalogs?page%5Bnumber%5D=3&page%5Bsize%5D=1"
}
}
However, when I make a Postman call to the "next" URL (http://localhost:3000/v1/catalogs?page%5Bnumber%5D=2&page%5Bsize%5D=1),
I get:
Started GET "/v1/catalogs" for 172.18.0.1 at 2017-09-08 15:27:04 +0000
undefined method `to_i' for #<ActionController::Parameters:0x0000c68977f718>
Did you mean? to_s
to_h
Is there something different that has to be done with Rails 5 params to get pagination for ActiveModel::Serializers to work?
It appears params[:page] doesn't hold the page number, but a "hash":
{ number: 1, size: 1 }
. That said, you want to use the page number as the argument topage
:Maybe even call
.per(page_params[:size])
too, to let the API change that as well.