Creating a Blog using Ruby and Rails- Part 2

Khemlall Mangal
5 min readFeb 12, 2021

Add to Github and deploy to Heroku before we start implementing other feature to our blog.

I like to save my work and publish often. This way i can resolve any issue easily rather than waiting until the end to commit. Lets start by creating repository in github. If you do not have an account already please got o github.com and create and account. There are free public accounts and you can opt to pay to create private projects.

Click on New and create a repository by providing a name. I created demoblog.

Adding SSH KEY to our development account.

Follow this guide to setup ssh key on your codeanywhere account. https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

  1. Once you have setup your ssh key on github, Open your terminal and CD to your blog
  1. Enter the following:
ssh -T git@github.com

If you see you have successfully authenticated, then you know you are all setup!!! If you run into issue, please post your question and i will try to assist or make a blog on setting this up.

Lets create a local git repository so we can manage our project. Navigate to your blog folder and type the following

git init

Now lets add what we just did to the repository with the following command

git add

Then lets get the status

git status 

Git commit -m “commiting my first blog”

git commit -m "adding my first comment and commit"

Now lets add our remote repository link

git remote add origin git@github.com:randi2160/mydoemolbogg.git

Pushing your code with the following

or push an existing repository from the command line

git remote add origin git@github.com:randi2160/demoblog.git
git branch -M main
git push -u origin main

Before you can deploy your code to heroku, we need to make a few changes

One thing we have to note is that we get sqlite with our rails install. Its easier to get started. However, when we will be pushing to production we will need to use Postgresql for example. Lets Setup Postgresql on our environment.

In your Gem file add two groups one from development and one for production.

gem ‘pg’, group: :production

Before we bundle to install we need to make sure we have Postgresql install on our environment. Follow the steps below.

Install PostgreSQL from PostgreSQL Apt Repository

PostgreSQL is available in all Ubuntu versions by default, but it doesn’t guarantee automatic updates when new releases come out. The local repository only has “snapshots” of a specific version. The best practice is to install the software from the PostgreSQL Apt Repository.

Step 1: Add PostgreSQL Repository

sudo apt-get install wget ca-certificateswget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Then, add the PostgreSQL repository by typing:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Step 2: Update the Package List

After adding the official PostgreSQL repository, make sure to update the package list. Doing this ensures you install the latest PostgreSQL package.

sudo apt-get update

To install PostgreSQL and the PostgreSQL contrib package (which provides additional features), use the following command:

sudo apt-get install postgresql postgresql-contrib

Check the available Version:

apt show postgresql

Install PostgreSQL Package

sudo apt install postgresql postgresql-contrib

To establish a connection with the newly set-up database, log into the postgres account with:

sudo su - postgres

Now open a postgress prompt using the command:

psql

Check Connection Information

If you are connected to PostgreSQL and want to see details of the connection, use the command:

\conninfo

Now lets continue with bundle install

Open up a new terminal and navigate to blog and run the following command

sudo apt-get install postgresql 
sudo apt-get install libpq-dev
gem install pg

Let start with deploying to Heroku in 4 step

step 1: Sign up for a new account → select Ruby as programming language

Step 2: Install Heroku Tool belt https://devcenter.heroku.com/articles/heroku-cli

curl https://cli-assets.heroku.com/install.sh | sh

step 3:

heroku login — interactive

Step 4: Create a heroku site

heroku create 
Lets rename the site to what you want
heroku rename <name you choose> example: heroku rename khemblog

One thing to note Heroku will warn of the following, so before check in set the following.

heroku config:set HEROKU_DEBUG_RAILS_RUNNER=1

Heroku recommends to have the following file “Procfile” https://devcenter.heroku.com/articles/ruby-default-web-server

***WARNING ****
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote:
https://devcenter.heroku.com/articles/ruby-default-web-server

Procfile

Set Puma as the server for your web process in the Procfile of your application. You can set most values inline:

web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

However, we recommend generating a config file:

web: bundle exec puma -C config/puma.rb

Make sure the Procfile is appropriately capitalized and checked into git.

Folder structure for Procfile

Config

Create a configuration file for Puma at config/puma.rb or at a path of your choosing. For a simple Rails application, we recommend the following basic configuration:

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end

Push site to Heroku

git push heroku main

Once completed you will be able to launch your site.

Reference: https://phoenixnap.com/kb/how-to-install-postgresql-on-ubuntu

References :
https://dev.to/shivashankarror/rails-6-using-images-with-webpacker-and-asset-pipeline-4gk3

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.