Lennakim

I am lennakim

0%

inherit_resources 添加分页功能

inherited_resources 可以为controller 自动添加 action.
kaminari是常用的分页插件.
inherited_resources 默认的 index action 是没有分页功能的, 这是个灾难, 添加以下代码为 index 实现分页.

1
2
3
class Car < ActiveRecord::Base
scope :recent, -> {order(created_at: :desc)}
end
1
2
3
4
5
6
7
8
9
10
11
12
class CarsController < ApplicationController

inherit_resources

protected

def collection
end_of_association_chain.recent.page(params[:page])
end

end

资料

Kaminari work with Inherited Resources