gpt4 book ai didi

ruby-on-rails - will_paginate 和inherited_resources 不能一起工作

转载 作者:行者123 更新时间:2023-12-04 05:49:05 25 4
gpt4 key购买 nike

Will paginate (3.0.3) 不适用于继承的资源 (1.3.1)。在我的 Controller 中:

protected
def collection
@posts ||= end_of_association_chain.paginate(:page => params[:page])
end

我在初始化程序中添加了 require 'will_paginate/array',但这并不能解决问题。我如何才能将分页和继承的资源一起工作?在我看来,我得到了错误
undefined method `total_pages' for #<ActiveRecord::Relation:0x00000004312e38>

最佳答案

这有点远,但我遇到了这个问题,结果是 CanCan,我正在使用的另一个 gem。这是我偶然发现的第一件事,所以我想有一天它可能会帮助某人。

退房:https://github.com/ryanb/cancan/wiki/Inherited-Resources

调用 load_and_authorize_resource加载集合,因此条件赋值的右侧永远不会在 collection 中运行方法。上面链接中描述的修复是跳过对集合操作的授权,并在 collection 中明确进行检查方法。

skip_load_and_authorize_resource :only => :index

protected

def collection
@posts ||= end_of_association_chain.accessible_by(current_ability).paginate(:page => params[:page])
end

我希望能帮助某人。

关于ruby-on-rails - will_paginate 和inherited_resources 不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10304182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com