administrate: Namespaced models not working
Per #871 and #179, it looks like namespaced models were worked on before, but I’m still getting the same issue. Going to the “New <model>” page or trying to edit an object that has PaperTrail results in:
uninitialized constant PaperTrail::VersionDashboard
Which looks to be the same namespacing issue as before. I’m on version 0.11.0 of the gem, as well as Rails 5.2.1 and Ruby 2.5.1.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 2
- Comments: 20 (3 by maintainers)
Adding a bit more of context.
The main issue is that Administrate assumes the names of models following Rails conventions only. There isn’t an official way of telling a dashboard that it should be serving a model different from the one named in the current URL.
Due to this, if you want to provide a dashboard for a model, you must ensure that all necessary pieces are in place, and have the expected names.
For example, for
PaperTrail::Version
you will need the following.The admin controller will be under the
Admin
namespace (or whatever you call it in your app), as well as thePaperTrail
namespace because it’s part of the model name:The route will reflect this namespacing too:
The dashboard will also need the
PaperTrail
namespace:With any other dashboards referring to your model via associations, use the option
class_name
to specify the model class:And that should be it.
The situation is not ideal, and it stems from the way that
ResourceResolver
works, which I think is a bit too clever. It’s related to https://github.com/thoughtbot/administrate/issues/405, where I propose a potential solution (which would need a PR). Still, this doesn’t mean yet that such a solution is going to happen any time soon. It requires a bit of thought, and for this we require a bit of time. Personally I’d rather prune the long list of issues a bit before introducing big changes. That will help us understand better what sort of use cases people are getting into.Or, someone could try implement the solution at #405, and at least we’ll have real code to discuss. Doesn’t mean it will definitely be merged! But at least it helps understanding the problem better.
Looks like I reached a workable state in a project combining Globalize v5.3.0, PaperTrail v10.3.1 (which both use namespaced models) with Administrate v0.12.0, using the following steps:
namespace :admin do root ‘pages#home’ ...
otherwise got errorPage
not found when running generator.rails generate administrate:install
. NB: it worked but gaveWARNING: Unable to generate a dashboard for PaperTrail::Version. Administrate does not yet support namespaced models. WARNING: Unable to generate a dashboard for Thought::Translation. Administrate does not yet support namespaced models
, etc (1 warning about PaperTrail and several others about Globalized models).rails generate administrate:dashboard PaperTrail::Version
,rails generate administrate:dashboard Thought::Translation
, etcdashboards/translations_dashboard.rb
gets generated successfully with a class such asThought::TranslationDashboard
and various configurations inside, but it is in the wrong directorydashboards
--> move it to a new sub-directory which matches the namespace:dashboards/thoughts/translations_dashboard.rb
, etccontrollers/translations_controllers.rb
-->controllers/thought/translations_controllers.rb
namespace :paper_trail do resources :versions end
,namespace :post do resources :translations end
, etcundefined method 'globalized_model_id'
, this is becauseThought::TranslationDashboard
was generated withFORM_ATTRIBUTES = %i[ globalized_model thought_id locale quote ]
. I removedglobalized_model
from this array and then the translation edit page could be loaded correctly.Some other configurations were then also needed (e.g for models using ActiveStorage attachments, or models with custom
to_param
method, etc), but these were not related to this namespaced models issue.In case anyone else is experiencing issues with getting the namespaced models to work both on development and in production, here’s what works for me on Rails 5.2.2 Let’s say you have a namespaced model called
Foo::Bar
Your controller should look like this:
If you are using administrate in namespaced mode (eg. if it’s accessible via
/manager
instead of/admin
), then just replaceAdmin
withManager
and move the controller toapp/controllers/manager/foo/bars_controller.rb
**NOTE**: If you define your controller as below, it won't work on production, at least not on rails 5.2.2
Your dashboard should look like this:
Your routes should look like this
I had the same issue as in the OP and could fix it in my case with same code as suggested (and didn’t encounter any refresh issues so far):
Should Administrate generate this automatically?
Regarding the problem with
Manyhands::Tenant
, it may have been because of the plural namespace. This has been fixed now with https://github.com/thoughtbot/administrate/pull/2261When setting up for
ActiveStorage
the instructions from Pablo above worked well but I had to scope the routes as follows:I have problem when namespaces are plurals. In my case shops
This is my very bad fix inside the gem