VQV03
VQV03

Reputation: 43

Kaminari Pagination TypeError

I`m using kaminari in rails 7 to make the apgination of my API, but when i try to see a page different of the first page, im taking a type error, when i try access "http://localhost:3000/products?page=3":

 TypeError (no implicit conversion of Symbol into Integer)

My paginated products index:

# GET /products
  def index
    page_number = params[:page].try(:[], :number)
    per_page = params[:page].try(:[], :size)


    @products = Product.all.page(page_number).per(per_page)

    paginate json: @products
  end

Anyone has a clue to how solve this?

EDIT: I created a initializer named api_pagination.rb and put on him:

ApiPagination.configure do |config|
  config.page_param do |params|
    params[:page][:number] if params[:page].is_a?(ActionController::Parameters)
  end
    
  config.per_page_param do |params|
    params[:page][:size] if params[:page].is_a?(ActionController::Parameters)
  end
end

And now i can access "http://localhost:3000/products?page%5Bnumber%5D=2&page%5Bsize%5D=12" , but it doesn't look right to me, or this is the right way?

Upvotes: 0

Views: 152

Answers (1)

Alter Lagos
Alter Lagos

Reputation: 12550

Maybe you need to set default values? Try with:

page_number = params[:page].try(:[], :number) || 1
per_page = params[:page].try(:[], :size) || 20

if that doesn't work, check what's coming in those number and size params or share more information of your issue, like the whole error stacktrace, even better the full request log.

Upvotes: 0

Related Questions