DEV Community

loading...

Rails Command Line Shortcuts // Re: Generate

gabbinguyen profile image gabbinguyen ・2 min read

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

Alt Text
Entering rails generate model will create:

  • Model file in the app/models directory
  • Migration file in the db/migrate directory

Alt Text
Alt Text

2. Controller

Alt Text
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

Alt Text

Alt Text

Alt Text

3. Resource

Alt Text
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

Alt Text

Alt Text

Alt Text

Alt Text

4. Migration

Alt Text
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.

Alt Text

Resources:

Discussion (0)

pic
Editor guide