-
Notifications
You must be signed in to change notification settings - Fork 5
Creating User and Admin Model using Devise Rails 4.0
This tutorial will show you how to create two models within devise. We will create a User and Admin model.
This tutorial was made using Rails 4 and Devise 3
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
$ 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 see the changes devise has made by visiting these addresses in your browser.
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>
<li><%= link_to "Admin", new_admin_session_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" %>
$ 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_up
http://localhost:3000/admins/sign_in
$ rails generate devise:views
You can see the views are generated for devise in the devise folder. "app/views/devise"
Next I would recommend looking at Adding a Username to the User & Admin model