新手问题 使用 will_paginate 分页问题

a8885313 · January 16, 2013 · Last by a8885313 replied at January 16, 2013 · 2703 hits

class BlogYear < ActiveRecord::Base default_scope :order => "name Desc" attr_accessible :name ,:blogs has_many :blogs end

使用 will_paginate 给 BlogYear 中 blogs 分页,

class Blog < ActiveRecord::Base belongs_to :blog_year attr_accessible :content , :title... end

并不能直接给 Blog 分页因为要根据 BlogYear 分组

blog_year.blogs.paginate(:page => params[:page])

感觉这个数据结构有问题……blog 不是已经有 created_at 了么,为啥要专门用一个 blog_year 的 model 呢?

#1 楼 @Rei 他的意思应该不是这样,还是要按全体分页,但是要在适当位置插入年份

#3 楼 @blacktulip blog_year 为了方便,blog_year 和 blog 是有主外键关系的

#3 楼 @blacktulip 是的,整体分页

Blog.paginate(:page => params[:page]).group_by(&:blog_year_id)

http://apidock.com/rails/Enumerable/group_by

赞同 #2 楼 @blacktulip

#6 楼 @Rei undefined method `total_pages'
View : <%= will_paginate @blogs %>

#7 楼 @a8885313

@blogs = Blog.paginate(:page => params[:page])
<% @blogs.group_by(&:blog_year_id).each do |blog_year_id, blogs| %>
 ...
<% end %>

<%= will_paginate @blogs %>

好了,谢谢

#8 楼 @Rei 好了,谢谢

You need to Sign in before reply, if you don't have an account, please Sign up first.