Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have an Active Admin nested and non-nested resource view?

A user has_many transactions. I have active admin currently set to nest the transactions under user for basic CRUD using belongs_to :user in admin/transactions.rb. I also, however, need a top level view for transactions that shows a subset of transaction records that span across users. How can I accomplish this second part?

like image 931
Eric M. Avatar asked May 05 '12 13:05

Eric M.


2 Answers

I think the best way now is to pass in the "optional" option:

ActiveAdmin.register Transactions do
  belongs_to :user, :optional => true
  ...
end

This way, you'll get to access all Transactions from the main navigation menu as well as the nested view under a particular User.

If you want to find more, you can refer to the source code under:

https://github.com/gregbell/active_admin/blob/0.4.x-stable/lib/active_admin/resource.rb

Line 131

def include_in_menu?
  super && !(belongs_to? && !belongs_to_config.optional?)
end
like image 116
Adrian Teh Avatar answered Nov 18 '22 18:11

Adrian Teh


You need to create two Active Admin resources that both refer to the same Active Record Model that needs nested and unnested routes.

The parent resource:

ActiveAdmin.register ParentClass do
end

The nested resource:

ActiveAdmin.register ChildClass do
  belongs_to :parent_class
end

The unnested resource:

ActiveAdmin.register ChildClass, :as => "All Children" do
end

You'll now have direct access to ChildClass via the "All Children" tab without getting an error that the ParentClass is missing while still enjoying the nested access to the ChildClass from the ParentClass.

like image 44
Matt Ridenour Avatar answered Nov 18 '22 18:11

Matt Ridenour