carpamon
carpamon

Reputation: 6623

ActiveAdmin: How to override index controller action: undefined method `base' for nil:NilClass

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'.

Upvotes: 7

Views: 15605

Answers (3)

Henry Le
Henry Le

Reputation: 1411

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

Upvotes: 3

davidcollom
davidcollom

Reputation: 413

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

Upvotes: 4

carpamon
carpamon

Reputation: 6623

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.

Upvotes: 11

Related Questions