Ruby 在 Rails 上使用 active_admin 和搜查,在 active_admin:resource 上出现分页错误
Ruby on Rails using active_admin and ransack, paging error on a active_admin:resource
错误:
集合不是分页范围。在调用之前设置 collection.page(params[:page]).per(10) :paginated_collection.
unless collection.respond_to?(:num_pages)
raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.")
end
@contents = div(class: "paginated_collection_contents")
application_controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :all_categories
helper_method :all_posts
before_filter :site_search
def all_categories
@categories = Category.all
end
def all_posts
@all_posts = Post.all
end
def site_search
@q = Post.ransack(params[:q])
#@posts_search = @q.result(distinct: true)
@search_posts = @q.result(distinct: true)
end
end
ActiveAdmin 资源:post
ActiveAdmin.register Post do
permit_params :title, :body, :category_id, :admin_user_id
menu :label => "Blog Posts"
index do
column :title
column "Author",:admin_user
column :category
column :created_at
default_actions
end
end
posts 控制器的索引方法:
def index
#@content_first = Post.find(1).title
#@content_second = "This is 2";
@q = Post.ransack(params[:q])
@posts = @q.result(distinct: true)
#@posts = Post.all
end
end
想通了。我没有完全改变模型。
对于那些有类似问题的人:
1.尝试重命名你的变量
2. 检查您的模型
错误: 集合不是分页范围。在调用之前设置 collection.page(params[:page]).per(10) :paginated_collection.
unless collection.respond_to?(:num_pages)
raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.")
end
@contents = div(class: "paginated_collection_contents")
application_controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :all_categories
helper_method :all_posts
before_filter :site_search
def all_categories
@categories = Category.all
end
def all_posts
@all_posts = Post.all
end
def site_search
@q = Post.ransack(params[:q])
#@posts_search = @q.result(distinct: true)
@search_posts = @q.result(distinct: true)
end
end
ActiveAdmin 资源:post
ActiveAdmin.register Post do
permit_params :title, :body, :category_id, :admin_user_id
menu :label => "Blog Posts"
index do
column :title
column "Author",:admin_user
column :category
column :created_at
default_actions
end
end
posts 控制器的索引方法:
def index
#@content_first = Post.find(1).title
#@content_second = "This is 2";
@q = Post.ransack(params[:q])
@posts = @q.result(distinct: true)
#@posts = Post.all
end
end
想通了。我没有完全改变模型。 对于那些有类似问题的人: 1.尝试重命名你的变量 2. 检查您的模型