同一资源的两个页面-ActiveAdmin [英] Two pages for the same resource - ActiveAdmin
问题描述
当前,我有 User
模型,该模型已在 user.rb
中注册为ActiveAdmin的新资源。生成的页面显示所有具有范围的用户( all
/ 新闻工作者
/ startup_employees
)。现在,我想为相同的资源和相同的作用域创建另一个页面,但是应该只有记录中的 waiting
字段设置为 true
(并且前一页应仅显示:waiting => false
)。我该怎么办?我知道我可以使用过滤器来做到这一点,但是我需要两个单独的页面,菜单中有两个链接。
Currently I have User
model, which is registered in user.rb
as a new resource for ActiveAdmin. Generated page displays all users with scopes (all
/journalists
/startup_employees
). Now I want to create another page for the same resource, and the same scopes, but there should be only records with waiting
field set to true
(and the previous page should displays only this with :waiting => false
). How could I do that? I know I could do that with filters, but I need two separate pages, with two links in menu.
//解决方案
它甚至比建议还简单(谢谢大家!):
It was even easier than advices (thanks guys!):
ActiveAdmin.register User, :as => 'Waitlist User' do
menu :label => "Waitlist"
controller do
def scoped_collection
User.where(:waitlist => true)
end
end
# code
scope :all
scope :journalists
scope :startup_employees
end
ActiveAdmin.register User do
controller do
def scoped_collection
User.where(:waitlist => false)
end
end
# code
scope :all
scope :journalists
scope :startup_employees
end
推荐答案
STI(单表继承)可用于在 Active中创建同一表/父模型的多个子资源 admin
STI (Single table inheritance) can be used to create multiple "sub-resources" of the same table/parent model in Active admin
-
在用户表中添加类型列作为字符串
Add a "type" column in user table as a string
将此添加到 User
模型以镜像具有类型字段的等待字段
Add this to User
model to mirror waiting field with type field
after_commit {|i| update_attribute(:type, waiting ? "UserWaiting" : "UserNotWaiting" )}
创建新模型 UserWaiting
和 UserNotWaiting
class UserWaiting < User
end
class UserNotWaiting < User
end
创建活动管理员
资源
ActiveAdmin.register UserWaiting do
# ....
end
ActiveAdmin.register UserNotWaiting do
# ....
end
您可以在控制台中运行首次同步
You can run a first-time sync in console
User.all.each {|user| user.save}
.. .........
..............
另一种方法是跳过类型列(步骤1,2和5),然后用范围解决其余问题。
Another way could be to skip the type column (steps 1,2 and 5) and solve the rest with scopes.
-
上面的步骤3和4
Step 3 and 4 above
然后创建范围
#model/user.rb
scope :waiting, where(:waiting => true)
scope :not_waiting, where(:waiting => false)
<$中的作用域c $ c>活动管理员
#admin/user.rb
scope :waiting, :default => true
#admin/user_not_waitings.rb
scope :not_waiting, :default => true
只需确保其中的其他范围等待/不等待
Just make sure the other scopes in these two pages are also filtered on waiting/not_waiting
这篇关于同一资源的两个页面-ActiveAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!