While creating a new Rails app is already easy enough, customizing just how that app is created can get tedious as the number of options grows inside the Ruby on Rails CLI.
This guide is a quick tip for automating some of the options you might normally pass when creating a new Rails application.
If you're new to Ruby on Rails, creating a new application can all take place with a single line of code on the command line.
$ rails new my_app_name
The built-in CLI (command line interface) that ships with Rails recognize this command as initialization of a brand new Ruby on Rails application. When this command is run, quite a large series of files and folders are produced in the directory of your choice. The great thing about this is how little effort it takes to start coding your idea right away which is arguably the reason why Ruby on Rails got so popular.
There may come a time when you might want to branch away from the defaults when creating a new app. To do this you could reach for a couple of options to tailor your workflow.
I previously wrote about rails application templates. I'll give a quick summary of what they are below.
Passing a template to your
rails new command prompts the Rails CLI to reference a series of options and methods inside a
template.rb file. In other tutorials and series on this blog/my YouTube channel I reach for a template I created called Kickoff Tailwind. This particular template allows you to pass your own preferences to the app you are creating. My template uses Tailwind CSS for example so there is a series of commands that ensures it gets installed before I touch a single line of code. This saves time, cognitive load, and general speed in getting a new idea into other people's hands.
In this guide/video the focal point will be the
.railsrc file. You can create this file in your home directory (
~/). For mac users, I know this works but I'm not 100% if Windows/Linux users can experience the same benefits. Apologies if not!
When creating a new Rails application you might find yourself passing a series of options every time you do. This can get cumbersome and make it hard to remember each time. Instead of remembering or referencing some notes, you can use the
.railsrc file to do it for you.
# .railsrc --database=postgresql --webpack=vue --skip-action-mailbox
Above I've passed some common options I tend to pass when creating new Rails applications. It's not much but these can be easy to forget and spell correctly each time. Having the
.railsrc file present and these options set you can now safely run
$ rails new my_demo_app
and get the same benefits of
$ rails new my_demo_app --database=postgresql --webpack=vue --skip-action-mailbox
That's all there is to it. If you're like me it makes a lot of sense to have this preset since I create a lot of new Rails apps for the sake of teaching others. This might not be an awesome feature to you if you're only using a couple of rails apps at all times but I wanted to share since I thought it was rather neat.
If you'd like to see more quick tips and features you might have not known about Ruby on Rails let me know below and I'll gladly keep sharing. This framework is such a pleasure to use. Features like this make it better and better.