← Home
rails

Rails Generate controller

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Controller

Rails Generate Controller (rails g controller) is the generator for a Rails Controller. Controllers act as a bridge between your models and views — it's responsible for fetching the appropriate model data, then rendering it into a corresponding view. To accomplish that, the controller also handles processing the incoming HTTP request.

Rails works best when you stick to REST-ful conventions for controllers; Most controllers will typically have some, or all, of the 7 REST-ful actions — index, show, edit, update, new, create, delete

There is nothing stopping you from defining custom actions though (apart from REST-ful design pedagogy). For example, the following controller generator -

rails g controller assets index update process_results

Will generate the following controller, with the custom process_results method -

class AssetsController < ApplicationController
  def index; end
 
  def update; end
 
  def process_results; end
end

Command Builder for rails g controller

rails g controller 

Controller name

Controller action(s)

Generator Options

Runtime Options

# Command Options
❯ rails g controller

Usage:
  rails generate controller NAME [action action] [options]

Options:
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
      [--skip-routes], [--no-skip-routes]                    # Don't add routes to config/routes.rb.
      [--helper], [--no-helper]                              # Indicates when to generate helper
                                                             # Default: true
  -e, [--template-engine=NAME]                               # Template engine to be invoked
                                                             # Default: tailwindcss
  -t, [--test-framework=NAME]                                # Test framework to be invoked
                                                             # Default:

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
Description:
    Generates a new controller and its views. Pass the controller name, either
    CamelCased or under_scored, and a list of views as arguments.

    To create a controller within a module, specify the controller name as a
    path like 'parent_module/controller_name'.

    This generates a controller class in app/controllers and invokes helper,
    template engine, assets, and test framework generators.

Example:
    `bin/rails generate controller CreditCards open debit credit close`

    CreditCards controller with URLs like /credit_cards/debit.
        Controller: app/controllers/credit_cards_controller.rb
        Test:       test/controllers/credit_cards_controller_test.rb
        Views:      app/views/credit_cards/debit.html.erb [...]
        Helper:     app/helpers/credit_cards_helper.rb