First make normal rails app with devise user model
- $ rails new awesome-appThis will create new app
- $ cd awesome-appIt will change the directory to our new project
Setup Gemfile
- Add - gem 'devise'and- gem 'sendgrid-ruby'to the Gemfile
- finally run - $ bundle installon terminal
Setup devise
- $ rails generate devise:installit will create- config/initializers/devise.rband- config/locales/devise.en.ymlfile and follow the instruction generated by above command
- Now generate devise user model with - $ rails generate devise User
- 
then go to app/models/user.rband make this file look like this:
 class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable
- 
Uncomment the Confirmablesection of migration file located on theapp/db/migrate
 class DeviseCreateUsers < ActiveRecord::Migration[7.0] def change create_table :users 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 t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end
- Then run - $ rails db:migrate
Set up SendGrid
- Create account on SendGrid
- Set up a sender identity on SendGrid by clicking this link: https://app.sendgrid.com/settings/sender_auth/senders 
- Create your API key by following this link: https://app.sendgrid.com/settings/api_keys 
- add - gem 'dotenv-rails'on development and test group on gem file and run- $ bundle install
- create - .envfile in your project root and add following line inside it- SENDGRID_API_KEY = 'YOUR API KEY'
- Then, modify this Devise config to use your SendGrid sender identity: 
 
# config/initializers/devise.rb
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
Set up Action Mailer
- create config/initializers/action_mailer.rbfile and add following code
 # config/initializers/action_mailer.rb
ActionMailer::Base.smtp_settings = {
    user_name: "apikey",
    password: ENV['SENDGRID_API_KEY'],
    domain: "your-domain.com",
    address: "smtp.sendgrid.net",
    port: 587,
    authentication: :plain,
    enable_starttls_auto: true
}
Now let Test it
- run the server on local - $ rails server
- go to link http://localhost:3000/users/sign_up on browser 
- After signing up we will get the confirmation email.
 
 
              


 
    
Top comments (0)