All for Joomla All for Webmasters
Ruby On Rails

Ruby On Rails: Customizing/Overriding Scaffold generation

In software development, a framework is an abstraction that provides you a general application. That application has a generic structure with features to avoid reworking on common tasks, like setup a database or creating tables/CRUD’s, for example. Unlike programming libraries, when you’re using a framework there’s a Inversion of Control (IoC), since the application flow is determined by the framework.

In Ruby on Rails, the creation of MVC (Model-View-Controller) files is usually made by the framework default generator. Since the flow is up to the framework (with convention over configuration), there are a set of default rules it takes on for generating theses files. For example, script files format is coffee script, css files are on .scss format and HTML template files are generated in .erb format. For big projects with a large number of entities, it will worth to customize/override that default generation of files.

You can customize some options on your config/application.rb file. For example, if you would like to change the script format to JS and avoid generating CSS files:

You can check another examples for customizing it here and the complete reference is available on Rails Config Guide (section 3.3). However, sometimes you wanna go further and also override the template files for the scaffold. Since Rails 3.0, you can override those files just placing the new ones on templates folder. The view files must be placed on “/lib/templates/erb/scaffold” directory and controller template file should be on “/lib/templates/rails/scaffold_controller” (if those folders doesn’t exists, you should create them).

You can copy the original files that Rails uses as templates and edit them as you want. The easiest way to find those files is locating railties gem folder (you can show the path on console running “bundle show railties”). The controller template file is located on “lib/rails/generators/rails/scaffold_controller/templates/controller.rb” and the view files are on “lib/rails/generators/erb/scaffold/templates”. You can copy the files you want to replace to your project lib/templates folder.

In a customizing example, suppose you’re using chosen-select gem and would like to, whenever you generate a model with an association attribute, it already came with “chosen-select” class on the form. After the changes, the partial form template file would look like:

The lines 24-26 were added to the original code in order to check if the attribute is an association, and in that case, display a select with the chosen class. It’s not necessary any additional change to make it work for the next generated views. In a future post, I’m gonna show a more advanced way to customize scaffold generation with a custom generator, giving you more control and making possible, for example, to create additional files among the default ones. If you have any doubts or suggestions, please use the comment area or contact me.

You Might Also Like

1 Comment

  • Reply
    Sannytet
    December 11th, 2018 at 14:53

    Nice posts! 🙂
    ___
    Sanny

  • Leave a Reply