Creating User and Admin Model using Devise Rails 4.0

In this tutorial I will show you how to install Devise. I decided to make this tutorial because it is hard to find a good resource for installing Devise on Rails 4.0. This tutorial will NOT teach you the basics of Ruby on Rails. It is intended to help those who have a basic understanding of Ruby on Rails.https://rubyonrailshelp.wordpress.com/wp-admin/post.php?post=12&action=edit&message=10

If you would like a good place to start learning Ruby on Rails, read and complete http://ruby.railstutorial.org/ruby-on-rails-tutorial-book by Michael Hartl

Other good resources are:
Devise Github page https://github.com/plataformatec/devise
Railscasts http://railscasts.com/

In this tutorial I will be using the following gems

  • Rails 4.0
  • Devise 3.2
  • PostgresSQL 9.1

Using PostgreSQL is optional. If you would like to use it then you can follow the railscast to learn how to set it up. http://railscasts.com/episodes/342-migrating-to-postgresql

This tutorial will show you how to create two models within devise. We will create a User and Admin model. I will also show you how set up a username for the sign in page.

Installing Devise

First we will install Devise.

$ gem install devise

This will install devise. After that we need to add devise into your Gemfile. This can be done by typing

$ gem 'devise'

within your Gemfile. Your Gemfile is within your application’s root folder. Next run the generator by using the command

$ rails generate devise:install

Generating a User Model

Next you should create a model called user.

$ rails generate devise user

Rails will create a user model and configure it with Devise modules. It will also create a migration file located in “db/migrate/devise_create_users.rb”. Run the command

$ rake db:migrate

When you run db:migrate, rails will create a table called users. It will display

==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
-> 0.0071s
-- add_index(:users, :email, {:unique=>true})
-> 0.0009s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0012s
==  DeviseCreateUsers: migrated (0.0101s) =====================================

Now the the model has been successfully generated. You can start the rails server by typing

$ rails server

or

$ rails s

To see what the site looks like you can go to http://localhost:3000/ or http://0.0.0.0:3000/ in your browser. You should see the rails home page when you open your browser.

You can see the devise generated sign-up and sign-in forms through these addresses.
http://localhost:3000/users/sign_up
http://localhost:3000/users/sign_in

You can add

<% if user_signed_in? %>

to the template. This will help to let us know whether devise is working or not. We can also add the link paths. This will allow us to get working links within our page.

new_user_session_path

will allow you to login and it will create a new session until you logout.

edit_user_registration_path

will allow us to change our password and delete our account.

destroy_user_session_path

will allow you to logout and it will destroy your current session.

Here is part of my header file which can be found in “app/views/layouts/_header.html.erb”

<ul class="nav pull-right">
  <li><%= link_to "Home",       root_path %></li>
  <li><%= link_to "Help",         help_path %></li>
  <% if user_signed_in? %>
    <li id="fat-menu" class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">
        Account <b class="caret"></b>
      </a>
      <ul class="dropdown-menu">
        <li><%= link_to "Profile", edit_user_registration_path %></li>
        <li><%= link_to "Settings", '#' %></li>
        <li class="divider"></li>
        <li>
          <%= link_to "Logout", destroy_user_session_path, :method => :delete %>
        </li>
      </ul>
    </li>
    <li> <%= link_to "Logout", destroy_user_session_path, :method => :delete %></li>
  <% else %>
    <li><%= link_to "Sign in", new_user_session_path %></li>
  <% end %>
</ul>

If a user is signed in; show a link to users, profile, settings, and logout. Otherwise show the link to sign in.

For the home page of my app I include the link to sign up. The link path to sign up is

new_user_registration_path

My example can be found in “app/views/static_pages/home.html.erb”

<%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-large btn-primary" %>

Generating an Admin Model

Next we will generate the admin model.

$ rails generate devise Admin

Then you should migrate the database by running

$ rake db:migrate

You should see something like this:

==  DeviseCreateAdmins: migrating =============================================
-- create_table(:admins)
-> 0.1063s
-- add_index(:admins, :email, {:unique=>true})
-> 0.0666s
-- add_index(:admins, :reset_password_token, {:unique=>true})
-> 0.0446s
==  DeviseCreateAdmins: migrated (0.2180s) ====================================

The generated admin model can be found here:

http://localhost:3000/admins/sign_in
http://localhost:3000/admins/sign_up

We also will generate the views for both of the models.

$ rails generate devise:views

You can see the views are generated for devise in the devise folder. “app/views/devise”

I have made a couple other tutorials which will be posted on this blog, or they can be found here: https://github.com/danweller18/devise/wiki

Advertisements

Tags: , ,

12 responses to “Creating User and Admin Model using Devise Rails 4.0”

  1. RSHKwee says :

    How can only an Admin create users? I have tried to find examples but have not found them….

  2. Nick M says :

    Very good stuff – spent a long time trying to get Devise to work before I found this tutorial.

  3. Mike Bethany says :

    Why have multiple models? That doesn’t seem very DRY.

    Instead add a role field to user. If you need multiple roles per user then create a roles model, a link table, and do has_many :through in the user and role models.

  4. hylozero says :

    Nice tutorial. Thanks.

  5. duard says :

    why 2 models ?

Trackbacks / Pingbacks

  1. Adding a Username to the User & Admin Model | Ruby on Rails Help - January 7, 2014

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

%d bloggers like this: