kid_drew
kid_drew

Reputation: 4005

Rails / ActiveAdmin - how to pass custom data from controller into content

I have a custom page that is going to pull some data and format it to so it can be displayed in a table. This is a very simplified version:

ActiveAdmin.register_page "My Custom Page" do
  controller do
    @data = [
      { name: "foo"},
      { name: "bar"}
    ]
  end

  content do
    panel "My Panel" do
      table_for @data do
        column("Name") { |row| row[:name] }
      end
    end
  end
end

When I run that, I get an error undefined method '[]' for nil:NilClass because the variable row getting passed into the block is nil. How do I properly pass the @data object to the view?

Upvotes: 1

Views: 863

Answers (1)

You can load the data within the view. While doing that you can access the params as if it were a controller.

content do
  data = Entity.find(params[:id]
end

This is not the Rails way, but I think within ActiveAdmin this is totally ok. If you had more complex data to load and process, you could create a service for it.

If you feel like you need to use use controller, you would do it like that:

controller do
  def index 
    # your code
  end
end

It is interesting, that you don't even need to call super - the page is rendered either way.

Upvotes: 1

Related Questions