简体   繁体   English

未定义的方法“分页”为

[英]undefined method `paginate' for

I am using Ruby on Rails 3.2.1 and Kaminari gem 0.13.0.我在 Rails 3.2.1 和 Kaminari gem 0.13.0 上使用 Ruby。

I have added gem 'kaminari' in my gemfile, then ran Bundle install .我在我的 gemfile 中添加了gem 'kaminari' ,然后运行Bundle install

In controller I have @posts = Post.order("name").page(params[:page])在 controller 我有@posts = Post.order("name").page(params[:page])

In view:在视图中:

<%= paginate @posts%>
        <% @posts.each{|posts| %>
            <h1 class="title"><%= link_to posts.title, posts %></h1>
                <p class="byline">Raksts izveidots: <%= posts.created_at.utc.strftime("%d.%m.%Y") %></p>
                <div class="entry">
                    <p><%= posts.content %></p>
                </div>
            <p class="meta"><a href="#" class="more">Read More</a> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <a href="#" class="comments">Comments</a> (33)</p>           
        <% } %>
        <%= paginate @posts%>

But in result I have undefined method 'paginate' for #<#<Class:0x3cf68b8>:0x2ab92b0> .但结果我有undefined method 'paginate' for #<#<Class:0x3cf68b8>:0x2ab92b0>

Anyone could help me?任何人都可以帮助我吗?

 class PostsController < ApplicationController
    # GET /posts
    # GET /posts.json
    def index
      @posts = Post.order(:name).page(params[:page]).per(2)
      @posts = Post.order('posts.id DESC')


      respond_to do |format|
        format.html  #index.html.erb
        format.json { render json: @posts }
      end
    end
def index
  @posts = Post.order(:name).page(params[:page]).per(2)

  respond_to do |format|
    format.html  #index.html.erb
    format.json { render json: @posts }
  end
end

I think you can do:我认为你可以这样做:

@posts = Post.order(:name).page(params[:page]).per(2), order: 'post_id desc',
                                                         per_page: 10

Hope this helps.希望这可以帮助。

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

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