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

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

 NoMethodError (undefined method `paginate' for #) 

产品控制器:

 require 'roo' class Api::V1::ProductsController  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', ' 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  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] 

谁能告诉我哪里出错了?

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

学分

更新:

 class ApplicationController include Rails::Pagination ...... .... end