I know there are a fair amount of Ruby on Rails tutorials, especially blog and devise tutorials. However, for me, all of these tutorials do not go into enough detail, especially if you are a beginner, or if you want to completely customize devise.
With this in mind, I wanted to create a step-by-step guide. The guide I wish I had when I was first starting to learn Ruby on Rails.
Note: This tutorial assumes you have Ruby on Rails up and running and have git installed.
I hope you enjoy it!
Initial work - make sure you are running Rails 5!
Open your terminal and run:
rails new devise_blog --database=postgresql cd devise_blog git init
Update Rails 5.2 - July 22, 2018
Please note, you can ignore the section below on installing your first gem if you are using Rails 5.2. If you are using Rails 5.2, please follow this guide.
# You will however still have to create your first commit: git commit -m ‘first commit - secured database credentials’
Now let’s install our first gem
We are going to install the dotenv gem to hide our database credentials. We are doing this so we can post our code on Github, which makes the app publicly available.
Now open your favourite text editor
I use Sublime Text, which is available for free here.
# Open your gemfile and add: gem 'dotenv-rails', groups: [:development, :test] # add it to the top of your gem file bundle # Now in the terminal create an .env file in your route directory devise_blog$ touch .env # Now open the file in your text editor and add: POSTGRESQL_USERNAME=your_username POSTGRESQL_PASSWORD=your_password # Now open your .gitignore file and add: .env # Next open your database.yml file and add: username: <%= ENV['POSTGRESQL_USERNAME'] %> password: <%= ENV['POSTGRESQL_PASSWORD'] %> # Now we can create the database with: rails db:create # ADDITIONAL SECURITY (NOTE: Now depracted in Rails 5.2) # We will also hide our secrets file, which holds our secret keys, open .gitignore again and add: /config/secrets.yml # This is an added security feature, but it is a best practice. # Now open config/application.rb and add the following line: module DeviseBlog class Application < Rails::Application config.secret_key_base = ENV["SECRET_KEY_BASE"] # This is the only line you need to add for the Devise gem. end end # Let's now run git status # Note if you see .env or secrets.yml still in the files to be committed run: git rm . -r --cached # This will clear the git cache # Now let's make our first commit git add . git commit -m ‘first commit - added the dotenv gem’
Now let’s add the app to Github
Open Github and click on start a new project after you login. In the repository name, type devise_blog, and add a description, such as: “A rails blog tutorial with a custom devise setup”.
Now add the remote destination in your terminal
devise_blog$ git remote add origin https://github.com/your-username/devise_blog.git git push -u origin master # You will be prompted to input your login credentials before the app compiles
Hosting on Heroku
If you don’t have a Heroku account you can set one up for free here.
# Open config/environments/production.rb and uncomment: config.force_ssl = true # Open your gemfile and specify your ruby version: ruby 2.4.0 # or whatever version you are running - save the file. # Now install the Heroku toolbelt for your machine -- Mac, Windows, Ubuntu etc. # Here is the link: https://devcenter.heroku.com/articles/heroku-cli # I am running Ubuntu on the Windows Subsystem for Linux, so my terminal command is: devise_blog$ sudo snap install heroku --classic # Now type in: heroku login heroku create devise_blog # Now your new app will be created with your link and git repo. # NOTE: if the name is taken just add your name to it like: brian_devise_blog #********NOTE: Skip the heroku config:set step if you are using Rails 5.2 as mentioned above. # Next, in the terminal, we have to tell Heroku about our username and password that we set up earlier: heroku config:set POSTGRESQL_USERNAME=your-username POSTGRESQL_PASSWORD=your-password #********END Rails 5.2 skip. # Verify it worked by typing: heroku config # This will show you your keys #********NOTE: Skip the below steps if using Rails 5.2 as mentioned above. # Next, since we hid our secrets.yml file in .gitignore, we need to create a new secret key for Heroku. rake secret # this will create a new secret key -- copy the output heroku config:set SECRET_KEY_BASE=paste-in-the-output-from-rake-secret #********END Rails 5.2 skip. # Lets now do a second commit before pushing our app to Heroku git add . git commit -m 'Specified our Ruby version to push the app to Heroku' git push heroku master heroku open # This will open the app in your default web browser, bookmark the link.
In the next post, we will be setting up devise for authentication.
Thanks for reading!