OK…. I wish I would have tried this earlier. It would have saved me alot of time. The default scaffolding that comes with Rails 2.3 is pretty good out of the box, but it is never going to fit exactly what you need for every app. One of my apps is a bit different than the default scaffolding setup. I have integrated Highslide Javascript Library into my app for the administration functions. It works very well. If you havent used it, its basically a lightbox script that allows you to use iframe content (and alot of other stuff) inside the modal popup. So when I need to edit or add something, I just click on it and a modal popup appears. When I save it, it closes the modal popup and refreshes the page via ajax or a meta refresh depending on the needs of that page. It gives you a nice smooth workflow. Some of the calls to highslide are baked into the controller and views. This is just one example of things I change from the default scaffolding. There are plenty of other things I like to add to every controller and view, such as my home-brewed security methods and a shared form for edit/new. All of this takes alot of time.
In the past I used scaffolding as my base point before I made all the changes. It was still faster than coding it from scratch, but what if I could make scaffolding generate exactly what I want every time?! It is actually much easier than I anticipated. It took me about as much time to change the scaffolding as it did to change one of the resources I created with the scaffolding. Here is what you need to do.
1. Locate your Rails source directory – on my Mac it is located here: /Library/Ruby/Gems/1.8/gems/rails-2.3.2 I use Aptana Studio for development (yeah I know.. I should use Textmate.. whatever dude), so I just added the rails source as a project so I could edit it directly from the IDE.
2. Go to this directory -> rails-2.3.2/lib/rails_generator/generators/components/scaffold/ and first take a look at scaffold_generator.rb. This class runs the scaffolding which creates all the files using the templates in the subdirectory /templates. The first thing I wanted to customize in the scaffolding was adding a partial named form. So I edited the method scaffold_views and added it to the end of the array like so:
#ADDED BY JOHN – _form
%w[ index show new edit _form]
end
Next I wanted to get rid of the layout file and css that are created when scaffolding runs. I rarely use them since I use the base layout and main css for my whole site, so I commented out these lines:
#m.directory(File.join(‘app/views/layouts’, controller_class_path))
……
# Layout and stylesheet.
#m.template(‘layout.html.erb’, File.join(‘app/views/layouts’, controller_class_path, “#{controller_file_name}.html.erb”))
#m.template(’style.css’, ‘public/stylesheets/scaffold.css’)
The possibilities in this file are endless. Just add/remove whatever you want to/from def manifest
3. Next you can edit the templates that you want to customize. /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/rails_generator/generators/components/scaffold/templates
I edited the controller template and all of the views. You will notice that the template uses ruby code for some of the generation:
<th><%= attribute.column.human_name %></th>
<% end -%>
Basically you can code it with ruby and it the generator will spit out a string which it writes to the .rb files. Think of it as code inside code.
I am not exactly sure what this means though, maybe someone can enlighten me: <%%=
I am assuming its something like escaping a quote.. but I dont really have a clue. I was still able to get it to output the code I wanted without knowing exactly what it does.
Anyways… You can change these files around to spit out the code you want. Its very easy to customize and it can save you a ton of time if you are a heavy scaffolding user. There is probably a way to turn this into a plugin or use a symlink, so you don’t have to edit the original files, but I guess I am lazy.