2015-12-05 65 views
0

我試圖使用自動完成與searchkick寶石,但我發現這個錯誤 未定義的方法`PAGINATE」爲# 這裏是我的post_controller.rb文件如何解決在軌道中Searchkick未定義的方法`paginate'?

class PostsController < ApplicationController 
 
    impressionist 
 
    before_action :set_post, only: [:show, :edit, :update, :destroy] 
 
    before_action :authenticate_user!, except: [:index, :show] 
 
    def index 
 
    @email = current_user.try(:email) 
 
    if params[:category].blank? 
 
    @posts = Post.search(params[:query]).paginate(page: params[:page], per_page: 10) 
 
    else 
 
    @category_id = Category.find_by(name: params[:category]).id 
 
    @posts = Post.where(category_id: @category_id).paginate(page: params[:page], per_page: 10) 
 
    end 
 
    end 
 
    def show 
 
    end 
 
    def new 
 
    @post = current_user.posts.build 
 
    end 
 
    def edit 
 
    end 
 
    def create 
 
    @post = current_user.posts.build(post_params) 
 

 
    respond_to do |format| 
 
     if @post.save 
 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
 
     format.json { render :show, status: :created, location: @post } 
 
     else 
 
     format.html { render :new } 
 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 
    def update 
 
    respond_to do |format| 
 
     if @post.update(post_params) 
 
     format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
 
     format.json { render :show, status: :ok, location: @post } 
 
     else 
 
     format.html { render :edit } 
 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
 
     end 
 
    end 
 
    end 
 
    def destroy 
 
    @post.destroy 
 
    respond_to do |format| 
 
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 
 
     format.json { head :no_content } 
 
    end 
 
    end 
 
    private 
 
    def set_post 
 
     @post = Post.find(params[:id]) 
 
     @comments = @post.comments.all 
 
     @comment = @post.comments.build 
 
    end 
 
    def post_params 
 
     params.require(:post).permit(:title, :description, :video, :category_id) 
 
    end 
 
    def video 
 
     self.link.split('/').last if self.link 
 
    end 
 
    def autocomplete 
 
     render json: Post.search(params[:query], autocomplete: true, limit: 10).map(&:title) 
 
    end 
 
end

和後.RB文件

class Post < ActiveRecord::Base 
 
\t is_impressionable 
 
\t has_many :comments 
 
\t belongs_to :category 
 
\t belongs_to :user 
 
\t searchkick autocomplete: ["title"] 
 
end

和index.html.erb文件

<%- model_class = Post -%> 
 
<% @title="Know Your Operating System-posts" %> 
 
<div class="page-header"> 
 
    <h1>Tips And Tricks For Your Operating System<small></small></h1> 
 
</div> 
 
<%= form_tag posts_path, class: "form-inline", method: :get do %> 
 
    <div class="input-group input-group-lg"> 
 
    <% if params[:query].present? %> 
 
     <div class="input-group-btn"> 
 
     <%= link_to "clear", posts_path, class: "btn btn-default" %> 
 
     </div> 
 
    <% end %> 
 
    <%= text_field_tag :query, params[:query], class: "form-control", id: "post_search", autocomplete: "off" %> 
 
    <div class="input-group-btn"> 
 
     <%= submit_tag "Search", class: "btn btn-primary" %> 
 
    </div> 
 
    </div> 
 
<% end %> 
 
<table class="table table-striped"> 
 
    <tbody> 
 
    <% @posts.each do |post| %> 
 
     <tr> 
 
     <td><%= link_to post.title, post_path(post) %></td> 
 
      <td> 
 
      <% if current_user.try(:email) == '[email protected]' || current_user.try(:email) == '[email protected]' || current_user.try(:email) == '[email protected]' || current_user.try(:email) == '[email protected]' || current_user.try(:email) == '[email protected]' %> 
 
       <%= post.impressionist_count %> 
 
      <% end %> 
 
      </td> 
 
     <td> 
 
      <% if post.user == current_user %> 
 
      <%= link_to t('.edit', :default => t("helpers.links.edit")), 
 
         edit_post_path(post), :class => 'btn btn-default btn-xs' %> 
 
      <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
 
         post_path(post), 
 
         :method => :delete, 
 
         :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, 
 
         :class => 'btn btn-xs btn-danger' %> 
 
      <% end %> 
 
     </td> 
 
     </tr> 
 
    <% end %> 
 
    </tbody> 
 
</table> 
 
<table> 
 
    <tbody> 
 
     <tr> 
 
     <td> 
 
      <%= will_paginate @posts, inner_window: 2, outer_windows: 2 %> 
 
     </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

這裏是我的寶石文件

source 'https://rubygems.org' 
 
ruby "2.2.2" 
 
gem 'rails', '4.2.1' 
 
group :production do 
 
    gem 'pg' 
 
    gem 'rails_12factor' 
 
end 
 
gem 'searchkick' 
 
gem 'impressionist' 
 
gem 'mysql2' 
 
gem "therubyracer" 
 
gem "less-rails" 
 
gem 'sass-rails' 
 
gem 'uglifier' 
 
gem 'coffee-rails' 
 
gem "twitter-bootstrap-rails" 
 
gem 'jquery-rails' 
 
gem 'turbolinks' 
 
gem 'jbuilder' 
 
gem 'sdoc', '~> 0.4.0', group: :doc 
 
gem 'devise' 
 
gem 'will_paginate' 
 
gem 'mail_form', '~> 1.5', '>= 1.5.1' 
 
gem 'simple_form' 
 
gem 'ckeditor' 
 
gem 'carrierwave' 
 
gem 'mini_magick' 
 
gem 'activeadmin', github: 'gregbell/active_admin' 
 
group :development, :test do 
 
    gem 'byebug' 
 
    gem 'web-console' 
 
    gem 'spring' 
 
end

我怎樣才能解決這個錯誤

+0

您是否安裝了'will_paginate' gem?如果是,是否在安裝後重新啓動服務器? – Tobias

+0

是的,我已經_paginate gem安裝,我已經重新啓動服務器後安裝它。我在 –

回答

0

posts_controller線8,更改

@posts = Post.search(params[:query]).paginate(page: params[:page], per_page: 10) 

到:

@posts = Post.search(params[:query], page: params[:page], per_page: 10) 

Searckick已經具有在search方法built-in support for will_paginate

+0

上面添加我的gemfile我嘗試過使用它,但它不工作。所以請給我另一個解決方案 –

+0

你有錯誤嗎?如果是,你可以分享它。 –

+0

不,我沒有收到任何錯誤。 –