Search code examples
ruby-on-railsruby-on-rails-2scaffolding

Customize generator templates in Rails 2


I'm adding new models to an old Rails 2 application, and I'd like to customize the scaffold templates. In the Rails guide, it says this:

In Rails 3.0 and above, generators don’t just look in the source root for templates, they also search for templates in other paths. And one of them is lib/templates.

Does that mean that Rails 2.0.2 only looks at the templates in the source root? Is there any way to customize those templates?


Solution

  • The best I could come up with was to just modify the templates in the Rails source root. This is a bit easier if Rails is installed using RVM under your user home folder. In order to keep the new versions of the templates under source control, I created symbolic links in the Rails source root back to the new template files in my application's source code.

    1. I created a new folder under my application root: lib/templates.
    2. I copied the original templates from the Rails source code:

      cd ~/myapp/lib/templates
      cp ~/.rvm/gems/ruby-1.8.6-p420/gems/rails-2.0.2/lib/rails_generator/generators/components/scaffold/templates original
      
    3. I created a new folder templates/custom and copied the controller template there. Then I edited the file with some new features and put a symbolic link to it in the Rails source code.

      cd custom
      ln -sf `pwd`/* ~/.rvm/gems/ruby-1.8.6-p420/gems/rails-2.0.2/lib/rails_generator/generators/components/scaffold/templates/