2017-08-08 94 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 

如果有人跟進一個解決錯誤的解決方案,我會接受它。對於需要更多非默認設置的其他人可能會有用。