Learning Ruby on Rails can be overwhelming, so this article provides some examples on frequently used command line shortcuts to help get you started. We're going to focus on the rails generate shortcuts.
Using rails generate (or rails g) can help cut down the amount of time typing out code needed to build out your application, but what should we use, and when? Let's break down each shortcut and see what is created when we invoke it.
The shortcuts that we will be reviewing are:
- Model
- Controller
- Resource
- Migration
1. Model
Entering rails generate model will create:
- Model file in the app/models directory
- Migration file in the db/migrate directory
2. Controller
Entering rails generate controller will create:
- Controller file in app/controller directory with actions included (new, create, and show in this example)
- Routes for each action in the config/routes file
- HTML view files for each action in the app/views directory
3. Resource
Entering rails generate resource will create:
- Model file in the app/models directory
- Migration file in the db/migrate directory
- Controller file in the app/controllers directory
- Resources in the config/routes file
4. Migration
Entering rails generate migration can create a variety of different outcomes such as adding a column to an existing table, renaming a table, destroying a column, and so on.
Resources:
Top comments (0)