ActiveAdmin:如何覆盖索引控制器操作:未定义的方法“base”对于 nil:NilClass

发布于 2024-12-31 21:36:59 字数 488 浏览 1 评论 0原文

我试图覆盖 ActiveAdmin 控制器的索引操作,以显示 current_user 的结果而不是所有结果。

controller do
  def index
    @user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
  end
end

访问ActiveAdmin时,抛出异常:

ActionView::Template::Error (undefined method `base' for nil:NilClass):
    1: render renderer_for(:index)

我正在使用rails 3.1和最新的ActiveAdmin版本。 gem“activeadmin”,:git => 'https://github.com/gregbell/active_admin.git'。

I'm trying to override the index action of the ActiveAdmin controller for it to display results for the current_user instead of all results.

controller do
  def index
    @user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
  end
end

When accessing ActiveAdmin, an exception in thrown:

ActionView::Template::Error (undefined method `base' for nil:NilClass):
    1: render renderer_for(:index)

I'm using rails 3.1 and the latest ActiveAdmin version. gem "activeadmin", :git => 'https://github.com/gregbell/active_admin.git'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2025-01-07 21:36:59

我不知道为什么,但

controller do
    def index
      index! do |format|
        @user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
        format.html
      end
    end
end

还是成功了。

I don't know why but

controller do
    def index
      index! do |format|
        @user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
        format.html
      end
    end
end

did the trick.

沫雨熙 2025-01-07 21:36:59

这不再需要了。

ActiveAdmin 0.4.4 现在支持范围查询,而无需覆盖此方法。
请参阅此处:http://activeadmin.info/docs/2-resource-customization .html#scoping_the_queries

如果您的管理员具有不同的访问级别,您有时可能会
想要限制他们有权访问的内容。假设您的用户模型有
正确的 has_many 关系,您可以简单地确定列表的范围
和发现者这样:

 ActiveAdmin.register Post do
     scope_to :current_user

     # or if the association doesn't have the default name.
     # scope_to :current_user, :association_method => :blog_posts
 end

This is not required any more.

ActiveAdmin 0.4.4 now supports scoping queries without overriding this method.
please see here: http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

If your administrators have different access levels, you may sometimes
want to scope what they have access to. Assuming your User model has
the proper has_many relationships, you can simply scope the listings
and finders like so:

 ActiveAdmin.register Post do
     scope_to :current_user

     # or if the association doesn't have the default name.
     # scope_to :current_user, :association_method => :blog_posts
 end
羁〃客ぐ 2025-01-07 21:36:59

让我们像这样覆盖操作:

controller do
  def scoped_collection
    # some stuffs
    super.where("type = ?", "good")
  end 

  # other stuffs
end

通过这种方式,您还可以使用您覆盖的新集合正常运行导出功能(到 xml、csv、...)。

在我的测试中,它仅适用于条件和范围,不适用于限制。

从此参考:https://github.com/activeadmin/activeadmin/issues/642

Let override the action like this:

controller do
  def scoped_collection
    # some stuffs
    super.where("type = ?", "good")
  end 

  # other stuffs
end

By this way, you also can run the export functions (to xml, csv, ...) normally with new collection that you have overridden.

In my test, it just works for where condition and scope, not for limit.

Refer from this: https://github.com/activeadmin/activeadmin/issues/642

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文