Add custom fields in user model created using devise

  1. After generating the custom views using devise gem using:
rails generate devise:devise users

2. Adding custom fields inside users model

rails generate migration addNameToUser name:string

rake db:migrate

3. Now add the custom field inside the view i.e: views/users/registrations/edit.html.erb

div class="field">
  <%= f.label :name %> <br/>
  <%= f.text_field :name, autofocus: true %>

4. To render customer views created in views/users/ make change inside the devise configuration file: ie. /config/initializers/devise.rb are change the following :

config.scoped_views = true

5.  Now you have to make changes inside controller/application.rb to allow the fields :

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?


  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name]) 



Run ruby scripts inside command prompt in windows

First you need to install ruby inside you windows system. I am assuming you have have already installed it.In place command prompt you can install ComEmu from

1. Now create a folder inside you drive and place your ruby scripts inside it.


mkdir ruby_scripts

cd ruby_scripts

Simplest Ruby scripts is:

#!/usr/bin/env ruby

print "Enter name? "
name = gets.chomp 
puts "your #{name}!"

and save this script in sample.rb inside ruby_scripts folder. Now, run the ruby script using

ruby sample.rb

Rails App with confirmation and login in one go

  1. Build new rails app with Mysql database.
rails new app -d mysql

2.  create an welcome controller with index method with its view index.html.erb 

rails g controller welcome index

Open routes file in config/routes.rb and set :

get 'welcome/index'
root 'welcome#index'

3. Add Devise gem in the Gemfile

gem ‘devise’

now look the devise gem version install that will be install on your system.

bundle list  do the after step 4

this command will version of the gems. you have to mention the devise version in you gemfile now, because after few years the devise gem version will be changed and the remote gem will be update. Now if you project get transferred to the new server and the new developer will run a bundle install command he will get the error which will take hours to resolve or he will have to check the version of the devise installed on the old server and then mention the same in the gemfile or the best practise is keep updating your/client project according to the latest gems version by bundle update.

4. Install Devise gem

bundle install

Install Devise , this will generate the necessary configuration files of Devise like devise.rb inside config/initializers/devise.rb

rails generate devise:install

5. Now multiple instruction will appear on command prompt add the following line inside config/environments/development.rb

config.action_mailer.default_url_options = { host: localhost, port: 3000} also define default URL options config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options={:host => ‘localhost’ , :port => ‘3000’} Inside app/views/layouts/application.html.erb set notice and alert messages above <%= yield %>

<p class="notice"><%= notice %></p>

<p class="alert"><%= alert %></p>

6. Generate user model with Devise gem

rails generate devise user

This creates the necessary field required by user model to use devise gem authentications

7. Edit edit the generate migration file inside db/migrations/******_devise_create_users.rb  and commencement the following lines. 

## 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

8. Now, inside welcome/index.html.erb place the link:

<%= link_to 'User sing_in', new_user_session_path %>


If you are using window you will receive this error

TypeError: Object doesn't support this property or method <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>

To resolve this remove  //= require_tree . from application.js inside app/assets/javascripts/application.js

and remove  *= require_tree .  from app/assets/stylesheets/application.css

Now Login , Sign_up,  will start working fine without any error’s.

9. Now we can populate our welcome/index.html.erb

<h1>Welcome to ruby Deise</h1>
<% if user_signed_in? %>
    <%= link_to 'Edit Profile' , edit_user_registration_path ,:class=>'navbar-link' %>
<%= link_to 'Log out', destroy_user_session_path, method: :delete , :class=>'navbar-link' %>
<% else %>
    <%= link_to 'Sign in', new_user_session_path,:class=>'navbar-link' %> 
    <%= link_to 'Sign  up', new_user_registration_path , :class=>'navbar-link'%>
<% end %>

Now, configuring Devise mailer to send the necessary emails in while registration and forget password

10. Edit environment/development.rb add the following configuration:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host:'localhost',port:'3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    user_name:      '',
    password:       'xyz@123',
    domain:         'localhost:3000',
    address:       '',
    port:          '587',
    authentication: :plain,
    enable_starttls_auto: true
I have mentioned the user_name ,password and host directly instead of it we can 
store these variable inside our ~/.bash.rc
export EMAIL=
export PASSWORD = password 
and use ENV['EMAIL'] ,ENV[PASSWORD] inside development.rb inplace of hard coding the value's.
11. Inside model user.rb add :confirmable

class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:confirmable

12. To generate the views which we are already using inside views/devise:
 rails g devise:views

Directories will be as:

13. Overriding mailer views and generating view inside views/users

rails generate devise:views users
Directories will be as 
if you came across this error: 
ActionView::Template::Error (undefined method `new_confirmation_path' for #<#<Class:0xa78d298>:0xa78c758>):

14.Then, Generate the confirmation views run and reconfirm that you have added :confirmable inside user model

rails generate devise:views confirmable

15. Finally run the pending migrations and change the current environment to development rake

db:migrate RAILS_ENV=development

Note: Please use a newly created gmail id or that id which you have not secured other mailer will work fine and it will show you that it is sending email but confirmation emails will not be sent.

15. Create user controller

rails generate user controller

and proceed.

Rails 5 with Devise and Functionalities

  1. Install devise an and testing

2. Add Admin Role model

3.  Add confirmation to user who can actively use the content on website

If you have already generated devise view’s referring in 1st point for the application you then only generate confirmation view By:

rails generate devise:views confirmable 

4. .  Require Admin to activate

Add existing/new project to GitHub/BitBucket

1. Create a new empty repository on GitHub.
2. open command prompt in windows or shell in unix
3. Move to Main directory of your Ruby or any other project.
4. git init
5. git add .
6. git commit -m “First Commit”
7. git remote add origin ‘copy the http URL from github/BitBucket’
if you want to verify you newly added URL
8 git remote -v
Push the change on remote machine
9 git push origin master

Validtions in Rails Model

Different Validations in Rails :

  1. validates_presence_of  :status
  2. validates_numericality_of :fingers
  3. validates_uniqueness_of :toothmarks
  4. validates_confirmation_of :password
  5. validates_acceptance_of :zombification
  6. validates_length_of :password, minimum: 3
  7. validates_format_of :email, with: /regex/i
  8. validates_inclusion_of :age, in: 21..99
  9. validates_exclusion_of :age, in: 0…21,
  10. message: “Sorry you must be over 21”

Including multiple validations in one line ( ‘ ,’  separated)

validates :status,   presence: true,
uniqueness: true,
numericality: true,
length: { minimum: 0, maximum: 2000 },
format: { with: /.*/ },
acceptance: true,
confirmation: true

Create a free website or blog at

Up ↑