如何在 Kaminari 中添加分页
How to add pagination in 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
像这样的东西应该可以工作
在你的类别控制器中
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 %>
如何在类别中为 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
像这样的东西应该可以工作
在你的类别控制器中
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 %>