December 08, 2019
rails new ems_helper --database=postgresql
This will generate the initial project structure. I will be using postgresql as my database.
I added the .env file as well the
dotenv-rails gem to allow the use
of the .env files to allow for easy local development for things that requires
logins or API keys.
# Gemfile# ...# Make sure it is the first gem added. This way gems that require privileges# can inherit from dotenv-railsgem 'dotenv-rails', groups: %i[development test]# ...
First things first, setup a Dockerfile in the root directory.
Basically, pull in Ruby 2.6.3, add nodejs, and add all packages is what this Dockerfile does.
docker-compose is a utility provided by Docker to pull in various components.
IE: Redis, Memcache, caching layer, PostgresQL, MySQL, MonogoDB databases, and various other web components.
Here is a link to my docker-compose.yml file which pulls in PostgresQL.
I pulled most of this config off the Docker official documentation.
Before we get into building the project, let’s take a look at some basic commands:
docker-compose down to bring down the container
docker-compose up --build rebuilds the container if you make changes.
docker-compose run web <command> will run the commands from inside the container.
If for some reason the items listed below do not work, try resetting the container.
Also, if you add something to the
Gemfile, to update the container you must do the following:
docker-compose run web bundle installdocker-compose down# You must rebuild the container because the Gemfile.lock has been updated as welldocker-compose up --build
Alright time to start everything up.
docker-compose up --build.
Then run the following inside a seperate terminal once that has all finished
docker-compose run --rm web rails db:setup
You will most likely run into an issue with postgres.
To fix this, navigate to
default: &defaultadapter: postgresqlencoding: unicodehost: <%= ENV['PG_HOST'] %>username: <%= ENV['PG_USER'] %>password: <%= ENV['PG_PASSWORD'] %>pool: 5development:<<: *defaultdatabase: myapp_developmenttest:<<: *defaultdatabase: myapp_test
Now here’s where it gets a little tricky.
In the root directory, create a file called
# .envPG_USER='postgres'# Docker does not use a password on the container for local developmentPG_PASSWORD=''# Change this to '' if not using docker. I have also set this manually inside of# the docker-compose.yml file.PG_HOST='db'
Docker documentation says to use ‘db’ as the host in the
You will run into an error if you do not do this. I spent over 2 hours debugging this. Not a fun time.
Now, you should be able to create the database!
# equivalent to rails db:create && rails db:migrate && rails db:seeddocker-compose run --rm web rails db:setup
Finally, in your browser navigate to
The “Hello, Welcome to Rails” page should appear!
If on Linux, for me there are some permissions issues with Docker.
chown -R . from the root directory after using Docker if you run into
I really enjoy using TailwindCSS so I added it into the application.
There’s a few steps to this.
yarn add tailwindcss - add the tailwindcss node module
Then, create a tailwind stylesheet called
Add the base classes.
Add a stylesheet pack tag inside the head of the view
<%# app/views/layouts/application.html.erb %><head># ...<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %># ...</head>
docker-compose run --rm web rails generate controller static_pages index
config/routes.rb and add the following
Rails.application.routes.draw do# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.htmlroot 'static_pages#index'end
<%# app/views/static_pages/index.html.erb %><%# ... %><body><div class="text-red-500">Hello there!</div></body><%# ... %>
And thats it! You’re now fully setup with Rails 6, webpack, TailwindCSS, Docker / docker-compose From here, its time to start parsing the API content. That will be the next installment I will write!
Special thanks to Chris @ gorails for his free content. He helped me with the tailwind setup. Tailwind Setup Video
Docker documentation is fantastic check it out. Docker Docs
Check out the Rails docs obviously Rails Docs
Finally, feel free to follow along with my repo. Most of the development will take
place on the
I create a Rails 6 skeleton for tailwind css HERE
Written by Konnor Rogers who currently works at VeueLive as a full time developer. You should follow him on Twitter