Rails3 Active Admin: How to display only Open status records when first click on Shipments tag?

此生再无相见时 提交于 2019-12-24 00:21:02

问题


I'm using ActiveAdmin. I have a list of Shipments with status (as a string) of Open and Closed. When the user clicks on the Shipments tab, I want to display only the Open shipments. How can I do that? Of course, the user could later choose to see the Closed shipments by using the filter. But I want the default to initially only show the Open shipments.


回答1:


Probably best way will be to create scopes in model. AA automatically gets your scopes and creates tabs above table in index view. Remember to add scopes in app/admin/your-resource-name.rb file.

#app/models/shipments.rb
  scope :opened, where(:status => "Open")
  scope :closed, where(:status => "Closed")

... and add scopes to resource file

#app/admin/shipments.rb
  scope :opened
  scope :closed

I don't have time to test, but it should work.

ASCIIcast with simple scope: http://asciicasts.com/episodes/284-active-admin




回答2:


scopes in the model:

#app/models/shipments.rb
  scope :opened, where(:status => "Open")
  scope :closed, where(:status => "Closed")

scopes in the activeadmin resource, one marked as default:

#app/admin/shipments.rb
  scope :opened, :default => :true
  scope :closed


来源:https://stackoverflow.com/questions/9486771/rails3-active-admin-how-to-display-only-open-status-records-when-first-click-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!