api-pagination gem 配置错误 Rails 5 部署到 heroku 时

api-pagination gem configuration error in Rails 5 when deployed to heroku

我正在使用 api-pagination gem 并在 rails 5 应用程序中部署到 heroku。我的配置在开发中没问题,但在生产中失败了。这是配置文件:

config/initializers/api_pagination.rb

ApiPagination.configure do |config|
  # If you have both gems included, you can choose a paginator.
  config.paginator = :will_paginate

  # By default, this is set to 'Total'
  config.total_header = 'X-Total'

  # By default, this is set to 'Per-Page'
  #config.per_page_header = 'X-Per-Page'

  # Optional: set this to add a header with the current page number.
  #config.page_header = 'X-Page'

  # Optional: what parameter should be used to set the page option
  config.page_param = :page
  # or
  config.page_param do |params|
    params[:page][:number]
  end

  # Optional: what parameter should be used to set the per page option
  config.per_page_param = :per_page
  # or
  config.per_page_param do |params|
    params[:page][:size]
  end
end

这是来自 heroku 日志的错误:

[INFO ] Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms) 
[FATAL] NoMethodError (undefined method `[]' for nil:NilClass):
[FATAL] config/initializers/api_pagination.rb:18:in `block (2 levels) in <top (required)>'

控制器操作现在非常简单:

  def index
    listings = Listing.where(active: true)
    paginate json: listings
  end

我没有在路由中包含任何参数。

在我的情况下,大部分配置都是不必要的。我不确定为什么它会导致错误,但我的案例的默认设置很好,除了我使用 will_paginate。这是我的配置文件并且有效:

ApiPagination.configure do |config|
  # If you have both gems included, you can choose a paginator.
  config.paginator = :will_paginate
end

如果有人跟进解决错误的解决方案,我会接受。它可能对需要更多非默认设置的其他人有用。