如何在Kaminari中添加分页

时间:2015-02-18 10:40:12

标签: ruby-on-rails pagination mongoid kaminari

如何为分类中的feed_entries添加分页?

我使用kaminari,mongoid 4,rails 4

Category.rb

class Category
  def feed_entries
    FeedEntry.in(source_id: sources.map(&:id))
  end
end

show.html.erb

<% @category.feed_entries.includes(:source).each do |feed_entry| %>
  <%= link_to feed_entry.name, feed_entry %>
  <%= feed_entry.source.title %>
<% end %>

模型

class Category
  include Mongoid::Document
  field :name, type: String
  has_many :sources, dependent: :destroy
end

class FeedEntry
  include Mongoid::Document
  field :name, type: String
  belongs_to :source, touch: true
  validates :source_id, presence: true
end

class Source
  include Mongoid::Document
  field :title, type: String
  has_many :feed_entries, dependent: :destroy
  belongs_to :category, touch: true
end

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效

你的类别控制器

 def show 
     @category = Category.find(params[:id]) 
     @feed_entries = @category.sources.includes(:feed_entries).page(params[:page])
 end

在视图中

<%= @feed_entries.each do |feed_entry| %>
  <%= link_to feed_entry.name, feed_entry %>
  <%= feed_entry.source.title %>
<% end %>
<%= paginate @feed_entries %>