简体   繁体   English

如何拥有Active Admin嵌套和非嵌套资源视图?

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

A user has_many transactions. 用户has_many交易。 I have active admin currently set to nest the transactions under user for basic CRUD using belongs_to :user in admin/transactions.rb. 我有活动管理员当前设置为使用belongs_to:user in admin / transactions.rb在用户下嵌套用于基本CRUD的事务。 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? 我怎样才能完成第二部分?

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 https://github.com/gregbell/active_admin/blob/0.4.x-stable/lib/active_admin/resource.rb

Line 131 第131行

def include_in_menu?
  super && !(belongs_to? && !belongs_to_config.optional?)
end

You need to create two Active Admin resources that both refer to the same Active Record Model that needs nested and unnested routes. 您需要创建两个Active Admin资源,这两个资源都引用需要嵌套和无效路由的相同Active Record Model。

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. 您现在可以通过“所有子项”选项卡直接访问ChildClass,而不会获得ParentClass丢失的错误,同时仍然可以从ParentClass享受对ChildClass的嵌套访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM