The original post where this came from can be found at Newbie Documentation Part I
Install AS in your project using the excellent instructions on the getting started page, http://activescaffold.com/tutorials/. Once you’ve installed the plugin, be sure you have a layout page (e.g., application.rhtml in the views/layouts folder) that at a minimum says something like the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <title>Owners: <%= controller.action_name %></title> <%= javascript_include_tag :defaults %> <%= active_scaffold_includes %> </head> <body> <%= yield %> </body> </html>
If you’re new to RoR, be sure your model displays properly using the default Rails scaffolding (see any RoR introductory text). Put the following line in your model_name_controller:
scaffold :model_name
NOTE: Since the default Rails scaffolding has been removed from Rails 2, it would be helpful to have another way to check whether everything is working. Also, saying “see any introductory text” is antithetical to the purpose of a Newbie guide. This guide should list every step needed, in precise and unambiguous language, not say something vague like “see any intro text”.
If everything works, turn on AS with the same line slightly modified:
active_scaffold :model_name
For the default scaffold, you don’t need a view (.rhtml) file of the same name as the model you’re displaying, but be sure you do have a layout file (see above). You can use a custom layout file by including the following line just before “active_scaffold :model_name” in the model_controller (where layout_name.rhtml is the layout file in the app/views/layouts folder):
layout "layout_name"
For custom layouts, here’s the drill (http://activescaffold.com/docs/):
<%= render :active_scaffold => 'model_name', :constraints => {hash of constraints},:label => 'Custom Scaffold Title' %>
The :constraints hash filters (limits) the records displayed, allowing you to change which records different classes of users can see. Using this approach, you can have as many different types of scaffolds on a page as you want.
If the only stuff on the custom page is the scaffold, fixed HTML text, and some links, you don’t need a controller method for that custom page in your model controller.
So far, I don’t know how to customize a given model scaffold for different users, aside from the varying constraints (different sets of records) offered by “render :active_scaffold” as described above. How does one vary the available fields? I’ve seen talk in the forums about how to do this, but I’ve not yet gotten that far. I suspect field and form overrides are one way. Somehow I doubt you could put a whole new scaffold within a method, or create a new scaffold within a duplicate (different name) model controller.
Also see Customizing Links in Active Scaffold
In my e-mail application, I put a Reply link to the right of each message in the List table (right where the Edit, Update, and Delete links go unless you remove them). Here’s how. Put the following within the active_scaffold :model_name do |config| block:
config.action_links.add 'reply_to_message', :label => 'Reply', :type => :record, :page => true
The Action Link documentation shows what the options mean, but “reply_to_message” is the method, “Reply” is the label, “:type => :record” puts the link to the right of each record (rather than once for the scaffold), and “:page => :true” tells AS we’ll be moving to a different view page. AS helpfully sends your method a params([:id]) containing the record id, free for nothing.
You can add more than one of these custom links. If you had to completely override one of the standard CRUD actions, I imagine you can turn it off (e.g., config.actions.exclude :create) and then write your own custom Create link. Or override the do_create method in your controller, adding a “super” at the end of the method when/if you want to return to default behavior.