1

私は、Rails 5に基づいたJSON APIを持っています。特定の会社のすべての製品のリストを提供する方法があります。一度にデータ。このため私は2つの宝石、すなわちwill_paginateapi-paginationを見つけました。Rails 5:NoMethod Error - 未定義のメソッドPaginate

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] 

bundle installを実行し、サーバーを再起動した後、私はまだ、次のエラーを取得しています誰かが私がどこに間違っているのか教えてくれますか?

答えて

2

ApplicationControllerActionController::BaseではなくActionController::APIから継承されていることを確認してください。なんらかの理由でコントローラを更新できない場合は、にRails::Paginationモジュールを手動で含めることができます。

credits

更新:

class ApplicationController 
    include Rails::Pagination 

    ...... 
    .... 
end 
+0

こんにちは、私はレールに新たなんだと、あなたのコントローラーを更新することはできません何らかの理由であれば、あなたはまた、」についての第二の点を詳しく説明することができ、 Rails :: PaginationモジュールをApplicationControllerに手動で組み込むことができます。 これはどのように行うことができますか? クラスApplicationControllerには:DEF はJSONレンダリングrender_404 null_session :::render_404と protect_from_forgery {エラーが: '要求されたリソースが見つかりません'}、ステータス:404 end end – Paras

+0

ありがとう、ありがとう – Paras

関連する問題