Brandon Harris | Why aren't you using Active Scaffold?

Why aren't you using Active Scaffold?

I do a lot of work creating admin interfaces in rails. A lot of it is redundant, so it would be nice to use a canned solution. Django has an admin interface built into every project. This makes a developers life easier.

I thought “Wouldn’t it be nice to have a plugin that read all my table associations and provided a simple crud interface?”

That is exactly what Active Scaffold tries to be.

How cool is active scaffold? Well, let’s pretend that we want to create the following tables:

 1 class CreatePeople < ActiveRecord::Migration
 2   def self.up
 3     create_table :people do |t|
 4       t.string :first_name, :last_name, :zip_code
 5       t.integer :title_id
 6       t.timestamps
 7     end
 8   #... and so on ...
 9   end  
10 
11 class CreateTitles < ActiveRecord::Migration
12   def self.up
13     create_table :titles do |t|
14      t.string :title
15      t.timestamps
16    end
17 end

Let's setup the following relationship in the Person model:

1 class Person < ActiveRecord::Base
2   belongs_to :title
3 end

We would have to write views for People and for Titles to create a robust and useful admin interface. With Active Scaffold you only have to do the following:

1. Include default javascript and active scaffold includes in the layout file:

2. Add the following to your people_controller and your title_controller respectively:

1 <%=javascript_include_tag :defaults %>
2 <%=active_scaffold_includes %>

And your done! ActiveScaffold takes care of the rest. The more skeptical among your are sneering right now, “what if I want to change the way the form is laid out, or what if I want to select a field from the title model instead of selecting an object….” You can do all of this with active scaffold and it isn’t super difficult.

To set the column order and have title appear as a drop down:

1 class TitlesController < ApplicationController
2   active_scaffold :titles
3 end
4 
5 class PeopleController < ApplicationController
6   active_scaffold :people
7 end

I recently had a record that was nested inside another record (such as title is nested inside people here) that had more columns than the horizontal space allowed. Since Active Scaffold uses ajax, a scrollbar wasn’t an option, so I found this:

1 active_scaffold :nested do |config|
2   config.subform.layout = :vertical
3 end

This sets this form vertical instead of horizontal in all forms that it belongs to. (That’s a mouthful).

Check it out, I am sure that you will find it useful.

Tags

  • e (1)