繁体   English   中英

Rails 5:NoMethod错误-未定义方法分页

[英]Rails 5 : NoMethod Error - Undefined Method 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]

有人可以告诉我我要去哪里了吗?

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

学分

更新:

class ApplicationController
  include Rails::Pagination

  ......
  ....
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM