Posts Tagged‘rails authentication’

Loading Video...
Close Video rails-authentication-with-devise

Rails Authentication With Devise

Play Video

The Devise Gem makes it really easy to create a full authentication app with Ruby on Rails. In the video below, I will walk you through setting up and installing the Devise Gem on your rails app. Below the video are all the commands and code that I used to create the application.

First off, we need to create our new rails application.

$ rails new devise

$ cd devise

$ mate .

Inside of your text editor, you’ll want to add the devise to your ‘Gemfile’, which is located in the application root directory. Add the following line to the ‘Gemfile':

gem 'devise'

Back in your terminal window:

$ bundle install

$ rails generate devise:install

$ rails generate devise user

$ rake db:migrate

$ rails g controller home

$ rails g controller dashboard

Inside of your ‘/app/controllers/home_controller.rb’ file, add the following:

def index
  if user_signed_in?
    redirect_to :controller=>'dashboard', :action => 'index'

Inside of your ‘/app/controllers/dashboard.rb’ file, add the following:

before_filter :authenticate_user!
def index    

Inside of your ‘/app/views/home/’ and ‘/app/views/dashboard/’ directories create a new file called ‘index.html.erb’ and add the following code into each file:

Welcome to our Website

Welcome to your dashboard

Now, inside of your ‘/app/views/layouts/application.html.erb’ add the following code below the opening ‘body’ tag:

<% if user_signed_in? %> Signed in as <%= %>. Not you? <%= link_to “Sign out”, destroy_user_session_path, :method => :delete %> <% else %> <%= link_to “Sign up”, new_user_registration_path %> or <%= link_to “sign in”, new_user_session_path %> <% end %>
<% flash.each do |name, msg| %>
  <%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>

Inside of your ‘/public/’ folder, rename the ‘index.html’ to ‘index.html.bak’.

Finally add the lines of code in your ‘/config/routes.rb’ file, below (devise_for :users)

resources :dashboard
root to: "home#index"

Back in your terminal start the rails server with this command:

$ rails s

And there ya go! It’s that simple to integrate the Devise Authentication Gem in your application :)