This guide shows the way I did to make a simple login app using Ruby on Rails with PosgreSQL and Devise. Some commands show in here may differ depending on your operation system. This guide was made using Ubuntu 18.04, Ruby 2.5.1, Rails 5.2.1, PostgreSQL 10.5, and Devise 4.5
If you don't have already installed there are several ways to do so, just go to the ruby install page.
For Debian/Ubuntu run sudo apt-get install ruby
Once you have install Ruby on your computer the next step is to install the rails gem install rails
This steps may differ depending on your operation system or distribution, visit the download page to know the installation process for you. If you have Ubuntu follow these steps:
- Create the file
touch /etc/apt/sources.list.d/pgdg.list
- Once you created the
pgdg.list
file add a line the repositorysudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
- Import the repository signing key, and update the package lists
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update
- Install PostgreSQL
sudo apt-get install postgresql-10
You might also install wget if you don't have it as is require to import the repository keys.
sudo apt-get install wget
In order to use PostgresSQL you will need to create a role for your user, if you don't know your user run whoami
, now to create a new role sudo -u postgres createuser --interactive
give it your user name and the superuser role.
Now that you have Postgres install you need to install the gem as well, this step is way more easy, like any other gem just run gem install pg
So far you should have everything install to set up the project. First we need to create a postgres role for the rails application, name the role as your app for example let's make a blog app, run createuser -dlP blog
and set a password for the role.
Now let's create the application, change to the folder where you want to install the project and run rails new blog --skip-coffee --database=postgresql
then cd blog
, this will change your current directory to the blog's project folder, inside this you will need to add the Devise gem to the Gemfile, this file is in the root of the project folder, open the project with your text editor and add to that file this line gem 'devise'
, save the file and run bundle install
to install the gem in the project.
Now we need to make the install of devise and the setup of the database, luckily this is accomplish with two commands, rails generate devise:install
and rake db:setup
, that is it, we have everything set.
I almost forgot that we need to include config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
in config/environments/development.rb
so open that file in your code editor and add the line before the end
of the block, I don't really know what is this for but that's want the installation instructions says.
Once everything is set we need to create the devise model for the User rails generate devise User
then migrate the database rake db:migrate
so far we have our authentication system working, if you want to see run rails server
(to stop the server press CTRL+C) then in your browser go to localhost:3000/users/sign_up
Now lets add a home page for some interactivity, this will be a very simple home page that will change depending on the user's session. First we need to create a controller for the home page rails generate controller Home index --no-assets
the last part of the command is because in this case we don't want rails to create default assets like css, js files as we won't need it.
When we run last command we tell rails that we wanted a Home controller with an index action this generates a route in config/routes.rb
open that file and change get 'home/index'
for root 'home#index'
this is for routing the home path to our home index view, by the way that view is in app/views/home/index.html.erb
open the file with your editor and change the code with the following one.
<div>
<h1>Welcome to the blog</h1>
<%= link_to "Sign up now!", new_user_registration_path %>
</div>
We are almost done, just need to add a navbar, as this will be code snippet present in all of the view lets put it in the application layout view so it will be render every time we load a view on the browser. Open the app/views/layouts/application.html.erb
file and paste the next code right after the <body>
tag.
<header>
<%= link_to "Rails Blog", root_path %>
<nav>
<ul>
<li><%= link_to "Home", root_path %></li>
<% if user_signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li>
<a href="#">Account</a>
<ul>
<li><%= link_to "Profile", '#' %></li>
<li><%= link_to "Settings", '#' %></li>
<li>
<%= link_to "Log out", destroy_user_session_path , method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", user_session_path %></li>
<% end %>
</ul>
</nav>
</header>
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message) %>
<% end %>
the resulting file should looks like:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<header>
<%= link_to "Rails Blog", root_path %>
<nav>
<ul>
<li><%= link_to "Home", root_path %></li>
<% if user_signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li>
<a href="#">Account</a>
<ul>
<li><%= link_to "Profile", '#' %></li>
<li><%= link_to "Settings", '#' %></li>
<li>
<%= link_to "Log out", destroy_user_session_path , method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", user_session_path %></li>
<% end %>
</ul>
</nav>
</header>
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message) %>
<% end %>
<%= yield %>
</body>
</html>
Now try it out, sign up a new user and you should be redirected to the home pages, notice the messages that appears and the links in the navbar section that changes depending on the user's session.
I hope that this guide really has helped you, this is the way that works for me. I know that, especially in the last part, there are a lot of thing that I didn't explain, I wanted to make this as brief as possible and to be honest I'm a beginner in rails, this began as a summarize and reminder to myself. Thanks for reading and if you want to learn more I recommend you The Rails Tutorial book.