With Rails API - TypeError (can't convert ActionController::Parameters into Integer)
belgoros opened this issue · 1 comments
I'm getting the below error when using with Rails 5.2.2
in API mode and active_model_serializer gem:
#adresses_controller.rb
def index
@addresses = Address.all.paginate(page: params[:page], per_page: 25)
json_response @addresses
end
Error stack trace:
Started GET "/api/addresses?page%5Bnumber%5D=2&page%5Bsize%5D=25" for 127.0.0.1 at 2019-02-25 10:01:39 +0100
(1.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by V1::Api::AddressesController#index as */*
Parameters: {"page"=>{"number"=>"2", "size"=>"25"}}
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms)
TypeError (can't convert ActionController::Parameters into Integer):
app/controllers/v1/api/addresses_controller.rb:7:in `index'
json_response
is just a concern method defined in controllers/concerns/response.rb
as follows:
module Response
extend ActiveSupport::Concern
def json_response(object, status = :ok, opts = {})
response = {json: object, status: status}.merge(opts)
render response
end
end
If the first 25 records are sent with success, the response erros happen when hitting next
link provided in JSON reponse:
http://localhost:3000/api/addresses?page%5Bnumber%5D=2&page%5Bsize%5D=25
When changing the URL manually from, next
provided in the JSON response:
http://localhost:3000/api/addresses?page%5Bnumber%5D=2&page%5Bsize%5D=25
to:
http://localhost:3000/api/addresses?page=2&per_page=25
, it works as needed.
What am I missing? Thank you.
Used versions:
Rails 5.2.2
Ruby 2.6.1
will_paginate 3.1.6
Hi, I can't tell from the provided information what exactly generated the ?page%5Bnumber%5D=2&page%5Bsize%5D=25
URL, but those query parameters are obviously wrong. The problem is in whatever generated that link. I don't think that happened inside the will_paginate library.