Hey there, Ruby friends! Do you remember our last journey together, when we created our My Amazing App with the simple command
rails new my-amazing-app? We unboxed that lovely piñata provided by Rails and found a whole bunch of directories and files—ready for us to start coding. But what if we want our piñata to have a different mix of candies or even some unique toys?
Well, that’s what we’re going to explore in this article! We’ll delve deeper into the magic behind
rails new and discover how to customize the gifts our generous Uncle Rails brings to our party. Ready? Let’s dive right in!
Beyond The Defaults
First, it’s worth mentioning that when you type
rails new my_amazing_app, Rails goes with the defaults it thinks are best for most applications. One of Rails’ principle is “Convention over Configuration” anyway, and this is a good example of that. These defaults include setting up a SQLite database, generating tests using MiniTest, and preparing an app ready for the browser.
But Rails is all about convention over configuration, right? What if we want to tweak those conventions? Well, there’s good news: we have plenty of options to customize our new app.
A quick tip before we start: if you want to know all the options you can use with
rails new, just type
rails new --help in your terminal. You’ll get a comprehensive list of options with a brief description of each one. But let’s talk about some of the most commonly used options.
Customizing with Rails New Options
One of the first options you might want to consider is
--ruby=PATH. This option allows you to specify the path to the Ruby executable that your application will use. This is particularly useful if you’re managing multiple Ruby versions with a version manager like RVM or rbenv.
Next up is the
--database=DATABASE option. With this, you can change the database your application will use. You can choose between PostgreSQL, MySQL, SQLite, and more. Just remember, if you choose anything other than SQLite, you’ll need to ensure the database server is installed and running on your machine. Most of the time my choice would be PostgreSQL. But you can choose whichever makes more sense for your project.
You might also find the
--skip-action-mailer option useful. If you know that your application won’t be sending any emails, you can use this option to tell Rails not to generate any setup for Action Mailer.
If you’re building an API-only application, you can use the
--api option. This will configure your application to start with a smaller set of middleware suitable for API-only apps.
Lastly, there’s the
--quiet option. This will suppress the output generated by the
rails new command, making your terminal output much cleaner. If you know exactly what you want and don’t need output text, this the option for you.
Whew! That was quite a deep dive, wasn’t it? The
rails new command is more than just a way to create a new Rails application. It’s a powerful tool that can set up your application just the way you like it, right from the start. And we only scratched the surface! There are many more options you can explore by typing
rails new --help in your terminal and play with different combinations of them.
Now, it’s your turn to go and create some amazing new Rails apps. Try different options, mix and match, and see what works best for you. Don’t forget to share your creations and experiences in the comments below. I can’t wait to see what you come up with!
As always, happy coding!