Rails 5:NoMethod错误-未定义方法分页 [英] Rails 5 : NoMethod Error - Undefined Method Paginate

查看:104
本文介绍了Rails 5:NoMethod错误-未定义方法分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Rails 5的JSON API.我有一个方法,可以带给一个特定公司所有产品的清单,我想对它进行分页,以免一次显示所有数据.为此,我遇到了两个宝石,分别是will_paginateapi-pagination. 运行bundle install并重新启动服务器后,我仍然收到以下错误:

I have a JSON API based on Rails 5. I have a method that brings list of all the products for one particular company, which I want to paginate so that not to bring up all the data at once. For this I came across two gems, namely will_paginate and api-pagination. After running bundle install and restarting the server, I'm still getting the following error :

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]

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

Can someone tell me where I'm going wrong?

推荐答案

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

Try to make sure your ApplicationController inherits from ActionController::API instead of ActionController::Base. And if for some reason you can't update your controller, you can include the Rails::Pagination module into your ApplicationController manually.

积分

更新:

class ApplicationController
  include Rails::Pagination

  ......
  ....
end

这篇关于Rails 5:NoMethod错误-未定义方法分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆