Rails Generators - Configuration

This blog is a continuation of Rails Generators - Controllers

As convenient as generators in Rails are it’s very easy to get carried away cluttering your app with unnecessary code. Thankfully Rails offers us multiple ways to avoid this issue by giving us the option to leave out unneeded files and functionality.

Configurations handled in the application.rb file.

One way of doing this is providing your generation commands with flags, for example -T is the flag for excluding test files. However, this soon gets tiresome as your app grows in size, but fear not! We can configure rails generators to conditionally exclude unneeded code. This is done from our app’s application.rb file located at config > application.rb.

By default depending on what version of rails you are using your application class will look something like this.

I have edited my application class and added the following…

So what is going on here? The config.generators do loop is fairly self-explanatory (configure every generator with the following arguments). In regards to the arguments provided this is where things may seem a bit fuzzy so I’ll go through them one by one.

This argument instruct rails to use the default connection to the database, it is rare you will need to change this unless you're using specific database types.

Here we keep rails default erb (embedded ruby) template engine, however, you could change this to something else like slim or haml.

A particularly useful configuration is choosing a testing framework, by default Rails uses test_unit, however, you can change this to something else like RSpec which is generally more favorable amongst Rails developers.

Here we simply exclude the generation of stylesheets, by default rails will generate SCSS files, however, they are not commonly desirable in a default state and can potentially override normal CSS.

Here once again we are simply excluding an item as we did with the SCSS, this time with rails default JS files.

Scaffold configurations handled in the lib

Rails by default has a number of default templates for scaffold generators.

You can see these within the official Rails GitHub.

We can however override these default configurations with our own custom ERB templates. Custom templates live in the lib directory of our application.

An example of how you could utilize this configuration for ERB goes as follows. In this example we are pointing to the index, however, this can be down for “show”, “edit” and all other Rails scaffolds.

  1. Create a directory within the lib folder named “templates”
  2. Within this directory create two more directories named “erb” and within “erb” create one more directory named “scaffold”. In summary, your file structure will be lib > erb > scaffold.
  3. Within scaffold create a file name index.html.erb.
  4. Within your index.html.erb create your own custom template, the default template is a follows.

More information on creating generators can be found on the Rails official website.

Hi, I’m Steven, most call me Steve! I’m a programmer, musician, and artist. This blog contains various tutorials and posts related to software development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store