0

Am working on a Modular Rails Application by following TutsPlus Modular Rails App training here.

However, after install Administrate Gem and running rails generate administrate:install to install my dashboard, I get the error as is in the image below:

enter image description here

In short Administrated Gem doesn't work with a Modular Rails App. How do I solve this problem or how do I make Administrate Gem support Namespaced Models???

What I have done:

I have tried to hard code a dashboard, but wont just work with Administrate (Rails just cant fine Administrate Gem in a Modular Rails App because of that error, but works with a Monolithic Rails App).

Afolabi Olaoluwa
  • 1,898
  • 3
  • 16
  • 37

4 Answers4

1

after merging this PR, Now Administrate supports the namespace resources. An example solution for the namespaced models is below. Let's suppose you have a post model in the blog namespace.

Model

app/models/blog/post.rb

Now you want to add it to the admin dashboard.
just follow this structure to make it work.

config/routes.rb

namespace :admin do
    namespace :blog do
      resources :posts
    end
    root to: "admin_users#index"
end

#app/admin/blog/posts_controller.rb

module Admin
  class Blog::PostsController < Admin::ApplicationController
  end
end

#app/dashboards/blog/posts_dashboard.rb

require "administrate/base_dashboard"

class Blog::PostDashboard < Administrate::BaseDashboard
  #normal stuff
end

For a detailed solution please check this discussion

0

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:

  1. 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
  1. 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
  1. 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
  2. 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
  1. 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.

seymore_strongboy
  • 934
  • 10
  • 16
-2

Read and follow carefully the instruction, then update me

What Is Administrate?

Administrate is a library for Rails apps that automatically generates admin dashboards. Administrate's admin dashboards give non-technical users clean interfaces that allow them to create, edit, search, and delete records for any model in the application.

Administrate solves the same problem as Rails Admin and ActiveAdmin, but aims to provide a better user experience for site admins, and to be easier for developers to customize.

To accomplish these goals, Administrate follows a few guiding principles:

  • No DSLs (domain-specific languages)
  • Support the simplest use cases, and let the user override defaults with standard tools such as plain Rails controllers and views.
  • Break up the library into core components and plugins, so each component stays small and easy to maintain.

Getting Started

Administrate supports Rails from 4.2, up to 5.0 and beyond.

Add Administrate to your Gemfile and re-bundle:

gem "administrate"

Then RUN bundle install in your terminal!!!

The included installer will create dashboards for each model in your app, complete with routes:

$ rails generate administrate:install

Restart your server, and visit http://localhost:3000/admin to see your new dashboard in action.

For more detailed instructions or to make it work with Rails API-only applications, please go through the 'Getting Started` guide.

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • 1
    Its not the first time am using Administrate Gem. Unfortunately, I see it work well for a Monolithic app rather than Modular App. On a normal day, it does as you posted but not for a Modular App. Am having Administrate gem namespace error. Its clearly stated in my question. – Afolabi Olaoluwa Nov 15 '17 at 09:13
  • @AfolabiOlaoluwaAkinwumi what the installer does is generating a controller right? It fails because you need to set that controller with namespace `admin::ApplicationController` inside `app/controllers/admin`, this is how their controller looks like https://github.com/thoughtbot/administrate/blob/master/app/controllers/administrate/application_controller.rb – Fabrizio Bertoglio Nov 15 '17 at 09:21
  • 1
    Its not working. really. Better still let me ask: In a case of a Modular App, where do I install my Administrate (In the parent directory or the engine)? – Afolabi Olaoluwa Nov 15 '17 at 14:22
  • @AfolabiOlaoluwaAkinwumi actually the error is about namespaced models not controllers. So probably you should check something about models too... I don't know.. you need to do some research deep in what need to be done to set your app .. .do you have gem alternatives? – Fabrizio Bertoglio Nov 15 '17 at 18:25
  • 2
    Will use alternative. Can be wasting time on this. Its clear as the Warning sounds the Administrate dont support namespace. – Afolabi Olaoluwa Nov 15 '17 at 21:55
-3

It supports namespace:

rails generate administrate:install --namespace=super_admin

To create Dashboard with namespace:

rails generate administrate:dashboard Model --namespace=super_admin
Muntasim
  • 6,689
  • 3
  • 46
  • 69
  • 1
    the dashboard itself isn't to be namespaced, the questioner wants to have a dashboard for an existing namespaced model in their app – Turgs Feb 22 '19 at 02:50