If you have been programming with Rails for a few months, you have probably heard of some of the drop-in administration frameworks. Include it into your app, and you have a simple interface for editing all your models — no trips to the database or console necessary.
Here is the situation: You have a model that must past administrator review, and use an
approved flag to filter the models; those that are approved are viewable on your site. Rather than code up a custom interface for displaying the models with an approved flag of false, you would rather use the administration interface to do the work for you. If you are already using ActiveAdmin, you can define a scope on a model and it will automatically add list filters to the interface. If you chose to use RailsAdmin (like me), that doesn’t work, you have to manually scan through the list to see those with the approved flag set to false.
Fortunately, it is an easy solution. Create a new model for your unapproved models,
UnapprovedPost for example. Inherit from your
Post model, and apply a default scope:
That’s it. Reload your server, and RailsAdmin will show a subresource under “Posts” for “Unapproved posts”, which only contain the posts where the approved flag is false.
But I can’t view the subresource in app now
Rails doesn’t know how to generate a URL for an UnapprovedPost, so you lose RailsAdmin’s “Show in App” link. We just need to tell Rails to use
Post for generating URLs:
Now if you open your console,
url_for will work correctly, and you will also have “Show in App” back.
Note: I have only tested this under Rails 3.2. It should work with Rails 3+, as both RailsAdmin and
default_scope are compatible. Rails 2.3 I cannot guarantee.