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:      'xyz@gmail.com',
    password:       'xyz@123',
    domain:         'localhost:3000',
    address:       'smtp.gmail.com',
    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= xyz@gmail.com
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
end

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

Directories will be as:
 views
           ->devise
                         ->confirmation
                         ->mailer
                         ->passwords
                         ->registrations
                         ->sessions
                         ->shared
                        ->unlocks

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

rails generate devise:views users
Directories will be as 
    views
          ->users
                  ->confirmation
                 ->mailer
                 ->passwords
                 ->registrations
                 ->sessions
                 ->shared
                 ->unlocks
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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: