Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't register Comment model #2493

Closed
meliborn opened this issue Sep 22, 2013 · 8 comments
Closed

Can't register Comment model #2493

meliborn opened this issue Sep 22, 2013 · 8 comments

Comments

@meliborn
Copy link

The goal is to overwrite some actions of comments_controller. So, I trying this:

ActiveAdmin.register Comment do
  controller do
    # some stuff here
  end
end

and getting uninitialized constant Comment. What the problem is? ActiveAdmin version is 0.6.0

@seanlinsley
Copy link
Contributor

The Comment model created by AA is actually namespaced as ActiveAdmin::Comment in Ruby and as active_admin_comments in the DB. However, AA comments are registered as Comments when it comes to the actual page registration. So in order to customize it, you need to do this:

ActiveAdmin.register ActiveAdmin::Comment, as: 'Comment' do
  # ...
end

To better understand how it all works, check out the source code.

@meliborn
Copy link
Author

Namespace, exactly. Thanks!

@seanlinsley
Copy link
Contributor

No problem

@meliborn
Copy link
Author

It's strange, but could you explain me why the code in controller part has no effect please?

ActiveAdmin.register ActiveAdmin::Comment do
  menu false

  controller do
    def create
      # Try to raise exception
      creat2e!
      @comment.r
    end
  end
end

@seanlinsley
Copy link
Contributor

You need to specify the :as option or it will register a completely new resource instead of overriding the existing one.

@meliborn
Copy link
Author

Like this?

ActiveAdmin.register ActiveAdmin::Comment, as: 'Comment' do
  menu false

  controller do
    def create
      # Try to raise exception
      creat2e!
      @comment.r
    end
  end
end

Done, but nothing changed.

@seanlinsley
Copy link
Contributor

Ah... the problem is we're using the after_load hook, which is run after your app/admin config files are loaded. So in order to override the behavior you have to use the after_load hook yourself:

ActiveAdmin.after_load do
  ActiveAdmin.register ActiveAdmin::Comment, as: 'Comment' do
    controller do
      def create
        raise 'ooooooo'
      end
    end
  end
end

@meliborn
Copy link
Author

Works like a charm. Thanks again ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants