Ruby on Rails

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'
  end
end

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

before_filter :authenticate_user!
 
def index    
end

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 <%= current_user.email %>. 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 :)

Rails Todo App with Scaffolding

In this quick video I wanted to demonstrate how quickly and easy it can be to create a small todo list app using Ruby on Rails. Below is the short video which will show you how you can create a todo list app in about 60 seconds.

Run the following commands in your terminal window:


$ rails new todo

$ cd todo

$ rake db:create

$ rails g scaffold task name:string status:boolean

$ rake db:migrate

$ mate .

Inside of your application directory you will want to add a line of code in your routes file located at /app/config/routes.rb

  root to: "tasks#index"

Then, inside of the /public/ folder you will see a file named ‘index.html’, and you’ll want to rename this to ‘index.html.bak’. Finally, go back to your terminal window and enter:


$ rails s

Now, you will be able to go to your http://localhost:3000 and you will have your new Todo list application in front of you. It’s just that easy ;)