devise
Rails Generate Devise (rails g devise
) is the generator for a Devise-backed Rails model
This generator will either add Devise to an existing model (ie: add the appropriate database columns), or create an entirely new model, with Devise pre-configured.
As with all the model-based generators, you can pass quite sophisticated arguments for the model fields —
# name has a 30 character limit and a unique index
rails g devise Product name:string{30}:uniq
# SKU refernces product model polymorphically with a standard index
rails g devise SKU product:references{polymorphic}:index
It's worth double-checking the migration Devise generates for you, since by default it adds a lot of columns to your model. There's also a few things you need to fill in, such as the #down
method body —
# Example Devise migration for a new model
class AddDeviseToAdmins < ActiveRecord::Migration[7.0]
def self.up
change_table :admins do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your original model.
# t.timestamps null: false
end
add_index :admins, :email, unique: true
add_index :admins, :reset_password_token, unique: true
# add_index :admins, :confirmation_token, unique: true
# add_index :admins, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
rails g devise
rails g devise
Model name
Model fields (as pairs)
Generator Options
Runtime Options
❯ rails g devise Usage: rails generate devise NAME [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated engines) [--skip-collision-check], [--no-skip-collision-check] # Skip collision check [--force-plural], [--no-force-plural] # Forces the use of the given model name [--model-name=MODEL_NAME] # ModelName to be used -o, --orm=NAME # Orm to be invoked # Default: active_record [--routes], [--no-routes] # Generate routes # Default: true ActiveRecord options: [--primary-key-type=PRIMARY_KEY_TYPE] # The type for primary key 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
Description: Generates a model with the given NAME (if one does not exist) with devise configuration plus a migration file and devise routes.