Admin of Rails Models

Ben Roesch edited this page Mar 31, 2015 · 1 revision

Storytime includes a generic admin functionality, similar to ActiveAdmin, allowing you to easily manage other models from your app within the CMS. To use this functionality you need to create a controller that inherits from StorytimeAdmin. To generate the controller, run the following StorytimeAdmin generator, replacing Widget with whatever existing model you would like to manage:

$ rails generate storytime_admin:resource Widget

The following file will be created:

# app/controllers/storytime_admin/widgets_controller.rb
module StorytimeAdmin
  class WidgetsController < StorytimeAdmin::ApplicationController
    set_tab :admin, :widgets
  end
end

If your Storytime user_class is not User, add storytime_admin.rb to your config/initializers folder and set the same user class for StorytimeAdmin:

# config/initializers/storytime_admin.rb
StorytimeAdmin.configure do |config|
  config.user_class = 'Admin'
end

Also, make sure your Storytime user model has an admin? method that returns true:

# app/models/admin.rb
class Admin
  storytime_user

  def admin?
    true
  end
end

You should see the model in the Storytime navigation menu with index / new / edit / delete functionality. You can customize views for any specific model (e.g. Widget) by adding a template to app/views/storytime_admin/widgets. See the source code for the various partials that get rendered when you visit the admin pages. You can also edit the controller that is generated by the install generator to change scopes, permitted params, or anything else you want to customize. There are comments in the template that gets installed to get you started if you want to change things, but you can override any action or method in our base admin controller by changing the controller for your specific model.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.