The ActiveScafold plugin for Rails promises to be a huge time saver.  In just a few easy steps, you can create a full web interface for your database, complete with inline editing and fold out panels.  Of course, it helps to have some grasp about what it is doing or you can get stuck like I did this morning.  I’m no expert (yet), but since it is so very cool, I wanted to share what I’ve learned (with the help of Sean Dick and Ivan Storck at tonight’s SFRuby Hack session).

After installing the plugin, there are just 3 lines of code that magically generate the HTML pages, but the trick is knowing where to put them. There’s a nice intro on the github wiki that outlines common use cases:

  • Prototyping
  • Admin Interfaces
  • Embedded, Widget-Style
  • Data-Heavy Applications

The use case that led me to ActiveScaffold today was the creation of an admin interface.  I’m working on a website and the end user stuff is pretty nice, but there are a bunch of tables where the data needs a little love… no one wants to launch the site without at least a few corrections in the data and it is crazy to either delay the launch while we build an admin interface or have an engineer make corrections with sql updates.  Enter ActiveScaffold: a way to allow admins to make the changes they need with very little software development.  (Later I expect we’ll need to add some fancy bits to the admin interface, but ActiveScaffold promises to be configurable and extensible enough when the time comes and the key point is that I don’t expect to need those features this week.)

ActiveScaffold for Admin

Make a little app for this experiment:

rails active_scaffold
cd active_scaffold
./script/generate scaffold Task title:string notes:text  complete:boolean
rake db:migrate

Install the plugin, which is compatible with Rails 2.3.2 (yay!) and previous versions of rails (if you install  a specific revision)

./script/plugin install git://github.com/activescaffold/active_scaffold.git

Now we have an app that lets you create, view, edit and delete tasks. This is the end-user app, you could edit the views and remove controller actions to prevent editing, deleting and/or creation. We want to leave this interface as is, but create a separate set of pages to allow an administrator to view, create, modify and delete tasks.

Sean came up with the idea of using routes with a namespace to facilitate this. Here’s what we came up with:

In config/routes.rb add the following code:

map.namespace :admin do |admin|
   admin.resources :tasks
end

Create a copy of /app/views/layouts/tasks.html.erb and call it admin.html.erb (in same folder), then add the following lines inside the <head> tag:

Create app/controllers/task_controller.rb:

class Admin::TasksController < TasksController
   layout "admin"
   active_scaffold :task
end

Check it out:

http://localhost:3000/admin/tasks


and when you click edit:

10 thoughts on “getting started with activescaffold

  1. Taking a quick look at streamlined, the same approach of namespacing admin versions of all of your controllers would still give you basically the same effect. If you wanted to be extra fancy, you could actually have your admin versions inherit from an admin controller:

    class Admin::AdminController < ApplicationController
    before_filter [:login_required, :must_be_admin]

    def must_be_admin
    # add something to the flash maybe?
    redirect_to “/” unless user.admin? and return
    end
    end

  2. Pingback: Double Shot #488 « A Fresh Cup

  3. Pingback: Daily Links #7 | :neil_middleton

  4. Pingback: rails admin interface roundup | the evolving ultrasaurus

  5. Nice comparative post, Sarah. I noticed that there is a slight typo in the line:

    Create app/controllers/task_controller.rb:

    which I believe should be:

    Create app/controllers/admin/tasks_controller.rb:

    Notice the folder “admin” where the controller should be along with the plural naming convention.

    Also, for Windows users, if you are having a tough time installing the active_scaffold plugin from Git, checkout the tip in my post at http://rails.webintellix.com/index.php/2009/05/installing-rails-plugins-from-github-on-windows/.

  6. That seems a little strange to have a tasks.html.erb and an admin.html.erb… Conceptually, I would expect it to be admin/tasks.html.erb.

    Does nesting everything behind /admin work well in practice?

  7. Pingback: Active scaffold | Kaokaokao

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required