activeadmin: Sidebar shown twice and wrong resource_path obtained for rails 4 in production.
I’m testing activeadmin with Rails 4 and got it working nicely in development. But in production, sidebar is shown twice. And the resource path is generated wrong in case of belongs_to dependency.
ActiveAdmin.register Tournament do
end
ActiveAdmin.register ScheduledMatch do
belongs_to :tournament
end
The admin path generated automatically for ScheduledMatch
by inherited_resources for actions, filter is admin_tournament_tournament_scheduled_matches_path
instead of admin_tournament_scheduled_matches_path
(Parent tournament is added twice) because of which it is failing in production.
The above problems show up only in production and not in development
My Gemfile (According to #2326 )
gem 'activeadmin', github: 'gregbell/active_admin', branch: 'rails4'
gem 'ransack', github: 'ernie/ransack', branch: 'rails-4'
gem 'inherited_resources', github: 'josevalim/inherited_resources'
gem 'formtastic', github: 'justinfrench/formtastic'
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 37 (14 by maintainers)
So how do I fix this bug? Upgrade activeadmin? To what version?
The patch works only onRuby > 2 and assumes that you don’t register the same resource with various definitions more than once. Having said that, here it is:
I’ve done a bit of digging with byebug, and found that as @marcroberts suspected files in
app/admin
are indeed being eager loaded.Unfortunately, the issue appears not to be because of config in the old app, but rather an issue with file naming.
For the fresh Rails 4 app, running
rails generate active_admin:install
createsapp/admin/admin_user.rb
. This file is only loaded once (by ActiveAdmin), and hence any calls to action_item only happen once.My existing app (for whatever reason) has several files that were plural rather than singular (in my case,
app/admin/tracks
). Files inapp/admin
in plural form appear to be loaded twice.The issue appears to be in
lib/active_support/dependencies.rb
on line 287. If the singular form is used (e.g. admin_user),search_for_file
incorrectly findsapp/models/admin_user.rb
If the plural form is used (e.g. admin_users),search_for_file
returns nil, hencerequire_or_load
attempts to requireadmin_users
, which then requires our file,app/admin/admin_users.rb
. As AA has already loaded this file, any calls to action_item happen twice, hence our duplicates.Although I haven’t tried it, I suspect this may also cause issues with any AA definition files that don’t have another file in
autoload_paths
with the same name.TL;DR, changing filenames to match those of the model (i.e. singular) seems to be a work around - renaming my file from
app/admin/tracks.rb
toapp/admin/track.rb
appears to have fixed the issue.