The first place a new developer encounters Rails Magic is in creating a new app.
It’s an incredibly simple command, with a dizzying amount of action behind it.
Assuming you have Ruby and Rails installed, you need only open a terminal window and type:
rails new blog_app
BAM! Rails app created.
You now have a massive “blog_app” directory full of files, 100 lines of command line output, and no clue what just happened.
How did the app get created?
Do you really need 40+ files for a simple blog app?
To understand how your Rails app was created, it’s helpful to look at the slow way of doing the same thing:
The Slow Way
1. Make a directory for your app and change into that directory
mkdir my_app
cd my_app
2. Create a subdirectory for each of the subfolders
mkdir app
mkdir config
etc.
3. Create the files that go inside the subdirectories, as well as the free-standing files that are not contained in a subdirectory
touch Gemfile (not inside a subdirectory)
touch routes.rb (not inside a subdirectory)
touch config/environments/development.rb (this file is inside the config/environments subdirectory)
etc.
4. Open each of those files and set some basic configuration settings, such as which database will be used and which gems (packages of pre-written code) will be included.
“Rails new” creates 40+ subdirectories and files, so doing this manually would take quite a while.
However, many of the files that it creates are only used in highly complex apps, and if it wasn’t so much easier to type “rails new”, you would probably create just a fraction of the files that are automatically created.
In the blog post Minimal Rails, Scott Raymond shows how you can create a Rails app with just 6 subdirectories: app, app/controllers, config, config/environments, log and public. It’s a great exercise for understanding the basic Rails framework.
Most apps will use more than the 6 subdirectories listed in Minimal Rails, but less than the “Rails new” command generates.
We’ll look into what each of those subdirectories does in a future post.