问题
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