This is a nifty scaffold generator created by Richard White (email, Height1Percent , MiniElements). It produces a CRUD AJAX table element in place of the standard Rails scaffolding.
This package (gem in ruby parlance) can be used in place of the existing Rails scaffolding to produce an AJAX style interface providing all the CRUD functionality you expect from scaffolding. It’s greatest use is (of course) at the start of a project to allow the rapid development of a site that is destined to be fundamentally AJAX based. However, by being selective about the parts of the generated code used this also provides a very good way to generate specific components for sections of an existing application.
It uses the new ‘rjs’ (JavascriptGeneratorTemplates) files and so requires Rails >=1.1, or Cody Fauser’s plugin
Here is the description from the rdoc documentation:
“Unlike conventional templates which are used to render the results of an action, these templates generate instructions on how to modify an already rendered page. This makes it easy to modify multiple elements on your page in one declarative Ajax response. Actions with these templates are called in the background with Ajax and make updates to the page where the request originated from.”
Cody Fauser has just (13/06/06) released an eBook on RJS through O’Reilly . This includes information on the Ajax Global Responders which could of been used to generate the spinning activity icon on the tables.
Code for the book (save on typing): RJS Code (See Comments section of Cominded for other examples)
The code makes good use of the Inflector class to generate more Human column titles.
Strengths
- Simple to use
- Sorting – the sorting has been implemented in a very nice way, the icons and highlighting of the sorted column are particularly noteworthy
- Pagination – works, what more can you say about pagination
- Design – lovely, makes heavy use of the Prototype library and the Rico library that builds on it.
Weaknesses
- Uses components which are due to be deprecated (These are easily replaced with partials however)
- Produces a lot of code if all you want is a list (no create / edit), however this can always be done in a parallel Rails project and the required code extracted
- Bookmarking / Back Button – as with any AJAX element navigation is an issue with the generated tables produced by this scaffolding (because you can’t bookmark the edit / create / delete pages directly, or indeed bookmark page 4 in a list).
Basic Usage
This outlines the out of the box behavior.
Running the following command:
rails script/generate ajax_scaffold User
produces:
controllers
users_controller.rb
helpers:
users_helper.rb
models:
user.rb
views/users:
_column_headings.rhtml
_user.rhtml
_form.rhtml
_form_messages.rhtml
_messages.rhtml
_new_edit.rhtml
_pagination_links.rhtml
cancel.rjs
component.rhtml
create.rjs
destroy.rjs
edit.rjs
list.rhtml
list.txt
new.rjs
update.rjs
views/layouts:
users.rhtml
lib:
ajax_scaffold.rb
public/stylesheets:
ajax_scaffold.css
public/javascript:
ajax_scaffold.js
rico_corner.js
Running ruby script/server and pointing the browser at http://localhost:3000 then gives:

Here the number of rows per page has been changed from the default of 25 to 10. This can be done by creating the following method in the DdTablesController.rb class
def default_per_page
10
end
At this point the table provides all the basic CRUD functionality we can create, edit delete and list – with column sorting and paging.
The main parts fo the code exist in the ajax_scaffold.rb file in lib. This Module defines the following classes
AjaxScaffold::ScaffoldColumn
AjaxScaffold::Model::ClassMethods
AjaxScaffold::Common
AjaxScaffold::Controller
AjaxScaffold::Helper
And extends
ActiveRecord::base
In fact the extension to ActiveRecord is achieved by using ‘extend Model::ClassMethods’ to give ‘build_scaffold_columns’ and ‘build_scaffold_columns_hash’
and then by defining two new class methods of ‘self.scaffold_columns’ and ‘self.scaffold_columns_hash’.
The AjaxScaffold::ScaffoldColumn class encapsulates the information required for a column in the ajax table: name, eval, label, sort_sql, sortable. An array of these is passed to the view.
The Common class contains two methods: current_sort, current_sort_direction which give access to the current session parameters for column sorting.
The Controller class provides a base class for any controller using the scaffolding in its views. This provides 4 methods: default_per_page (sets number of rows – default 25), update_params (get new params from request), store_or_get_from _session, clear_flashes.
Finally the Helper class defines several formatting methods as well as a set of id generators for the css.
The generated UsersHelper module extends the AjaxScaffoldHelper to provide information about the number of columns in this table.
Nesting

Richard covers the nesting of tables very clearly in his docs
Further Thoughts
While using the scaffold a couple of thoughts came to me:
- Where is the search?
- Wouldn’t I prefer the same functionality but delivered as a plugin (so I can just add ajax_scaffold :user, to my controller to get the functionality)?
- What about the back button and bookmarking?
- What about themes?
- Why aren’t the columns configurable in the controller?
So I have started to implement this. AjaxTable is on its way. At the very least it gives me an excuse to create a Rails plugin, and who knows it may even be useful!!!










One Comment
This is all extremely sweet stuff. I can’t wait to see AjaxTable.