If you're trying to figure out how to add a single-table inheritance (STI) model to your Administrate dashboard, here is how I was able to accomplish this using version 0.18.0
of the Administrate gem:
- Use the gem's generator to scaffold out a new controller and dashboard object for the base class of your model:
rails generate administrate:dashboard Foo::Base
- Add a namespaced route definition to your
routes.rb
file:
Rails.application.routes.draw do
...
namespace :admin do
...
namespace :foo do
resources :bases
end
...
end
- Move the newly-generated controller to a subfolder with the name of your models namespace. For example,
controllers > admin > bases_controller.rb
becomes controllers > admin > foo > bases_controller.rb
- Update the controller by overriding the
find_resource(param)
method. Note the use of becomes
This is necessary because without it, the Administrate gem will try to generate paths based on the specific model instances class (like Foo::Bar
), but above in the routes.rb
file, we've only added routes for the Foo::Base
class.
module Admin
class Foo::BasesController < Admin::ApplicationController
...
def find_resource(param)
resource_class.find_by!(id: param)
.becomes(resource_class) # this ensures that the resource is presented as an Foo::Base,
# not as an Foo::Bar, Foo::Baz, etc.
end
- Finally, you'll need to add a custom inflector to handle the pluralization of the word
Base
. Without it, for some reason, Rails pluralizes Base
to Basis
instead of Bases
(the latter of which Administrate expects). Without this, you'll see error messages like:
NameError (uninitialized constant Foo::BasisDashboard):
That's it!
I should make a PR to update the generator's behaviour to do this automatically for STI models, but in the meanwhile, these work-arounds got my namespaced model added to a dashboard.