ActiveAdmin:如何覆盖索引控制器操作:nil的未定义方法`base’:NilClass

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

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

访问ActiveAdmin时,抛出exception:

 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'

这不再是必需的。

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 

我不知道为什么

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

做了伎俩。

让我们覆盖这样的动作:

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

通过这种方式,您还可以使用已覆盖的新集合来运行导出函数(到xml,csv,…)。

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

请参阅: https : //github.com/activeadmin/activeadmin/issues/642