Paginate最近的帖子与kaminari

时间:2011-07-24 21:46:39

标签: ruby-on-rails-3 pagination kaminari

我正在使用rails gem kaminari(https://github.com/amatsuda/kaminari)来分页我的帖子数据库。目前我的控制器中有代码@posts = Post.order('id').page(params[:page]).per(5),但这会将页面从最早到最近排序。如何从最近到最早撤销此订单和订单?

2 个答案:

答案 0 :(得分:6)

您可以在模型中

default_scope order("created_at DESC")  

default_scope order("created_at ASC")  

答案 1 :(得分:1)

def index
    @all = Model.all
    @all = Model.order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end

def sort_column
    Model.column_names.include?(params[:sort]) ? params[:sort] : "updated_at"
end

def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "DESC/ASC"
end