2017-08-08 93 views
0

我正在使用api-pagination gem并在rails 5应用程序中部署到heroku。我的配置在开发中没问题,但在生产中失败。下面是配置文件:Rails 5中api-pagination gem配置错误部署到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 

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

回答

0

在我的情况下,大部分配置是不必要的。我不知道为什么它会导致错误,但我的情况的默认情况很好,除了我使用will_paginate。这是我的配置文件,它的工作原理:

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

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