← Home

Rails Generate devise:controllers

Reference & Command Builder

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

Rails Generate Devise:Controllers

Rails Generate Devise:Controllers (rails g devise:controllers) is the Devise Controllers generator; invoking it will copy all the Devise controllers into your Rails app, letting you override them.

The controllers this command generates will all be namespaced under the scope you specify, and will inherit from their corresponding Devise controller. For example, here's an example sessions_controller for the user scope -

class Users::SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  # def new
  #   super
  # end

If you choose to override the Devise controllers, you need to tweak your routes.rb file to use your new controller —

# routes.rb
devise_for :users,
            controllers: { sessions: 'users/sessions' }

From there, you can begin to modify and extend your new controller. This generator is similar to the Devise:Views generator, since both create Devise resources to override default behaviour.

Typically you don't want to override all the Devise controllers, just one or two. In that case, use the --controllers= option to specify which ones to generate (sessions is the usual suspect).

Command Builder for rails g devise:controllers

rails g devise:controllers 

Controllers scope (should match a model)

Generator Options

Runtime Options

# Command Options
❯ rails g devise:controllers

  rails generate devise:controllers SCOPE [options]

      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
  -c, [--controllers=one two three]                          # Select specific controllers to generate (confirmations, passwords, registrations, sessions, unlocks, omniauth_callbacks)

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
  Create inherited Devise controllers in your app/controllers folder.

  Use -c to specify which controller you want to overwrite.
  If you do no specify a controller, all controllers will be created.
  For example:

    rails generate devise:controllers users -c=sessions

  This will create a controller class at app/controllers/users/sessions_controller.rb like this:

    class Users::SessionsController < Devise::SessionsController