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