Rails 5:NoMethod错误 - 未定义的方法Paginate

时间:2017-07-30 08:59:30

标签: ruby-on-rails json pagination ruby-on-rails-5 will-paginate

我有一个基于Rails 5的JSON API。我有一个方法可以为一个特定的公司提供所有产品的列表,我想分页,这样就不会一次显示所有数据。为此,我遇到了两个宝石,即will_paginateapi-pagination。 运行bundle install并重新启动服务器后,我仍然收到以下错误:

NoMethodError (undefined method `paginate' for #<Api::V1::ProductsController:0x007fb25404db30>)

产品控制器:

require 'roo'
class Api::V1::ProductsController < ApplicationController
  respond_to :json

  def index
    comp_id = params[:company_id]
    cat_id = params[:category_id]
    if comp_id
      if comp_id && cat_id
        product = Product.where(:company_id => comp_id, :category_id => cat_id)
      else
        product = Product.where(:company_id => comp_id)
      end
      paginate json: product, status: 200
    else
      render json: { errors: "Company ID is NULL" }, status: 422
    end
  end
end

Gemfile.rb

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
#Devise Gem for Authentication
gem "devise"
#For JSON Serialization
gem 'active_model_serializers'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'carrierwave'
gem  'roo'
#For Pagination
gem 'will_paginate'
gem 'api-pagination'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  # Use Capistrano for deployment
  gem 'capistrano', group: :development
  gem 'capistrano-rails', group: :development
  gem 'capistrano3-puma', group: :development
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

有人可以告诉我哪里出错了?

1 个答案:

答案 0 :(得分:4)

尝试确保ApplicationController继承自ActionController::API而不是ActionController::Base。如果由于某种原因您无法更新控制器,则可以手动将Rails::Pagination模块包含在ApplicationController中。

credits

<强>更新

class ApplicationController
  include Rails::Pagination

  ......
  ....
end