Skip to content

Instantly share code, notes, and snippets.

@chair28980
Last active June 15, 2022 16:36
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chair28980/acaa7c8f8a71dc663194ce79c44d00b9 to your computer and use it in GitHub Desktop.
Save chair28980/acaa7c8f8a71dc663194ce79c44d00b9 to your computer and use it in GitHub Desktop.
New rails 6 app with postgresql and heroku deployment

Create a new rails 6 app with postgresql and heroku deployment

Before we begin

This guide aims to quickly take you thru the requisite steps to spin up a new rails app and easily deploy it using heroku.

Check your local

Lets check our local environment to make sure we're ready to go.

Check your ruby version (should be 2.6 or higher):

ruby -v

Check your rails version (should be 6.0 or higher): rails -v

Check your heroku cli version (v 7.35 at time of writing): heroku -v

Lets begin

Make sure you are logged into heroku: heroku login

Create new app: rails new myapp --database=postgresql

Make sure to cd into your app, then:

Create a welcome page: rails generate controller welcome

Modify app/views/welcome/index.html.erb to say something nice.

Add a root route to config/routes.rb: root 'welcome#index'

Setup the database: rails db:setup

Migrate the database: rails db:migrate

Verify everything is running: rails server

Make sure you are running the same ruby version locally as what is specified in the Gemfile.

Make sure project is all setup with git.

Deploy the app to heroku: heroku create

You can verify that the remote was added to your project by running: git config --list | grep heroku

Deploy your code: git push heroku master

Migrate the database (heroku): heroku run rake db:migrate

Make sure you are using the free version: heroku ps:scale web=1

Check the state of your heroku dynos: heroku ps

Visit the app in your browser: heroku open

View the logs: heroku logs

Tail the logs: heroku logs --tail

Run the rails console (heroku): heroku run rails console

Run rake commands example: heroku run <some command>

Create a Procfile in the root of the app directory touch Procfile

Insert the following into the Procfile: web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

Set your enviroment, from root project directory run:

echo "RACK_ENV=development" >>.env
echo "PORT=3000" >> .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "add .env to .gitignore"

You will need to setup a tmp/pids directory to run things they way we are setting them up here: mkdir -p tmp/pids then: touch tmp/pids/.gitkeep

Start webserver: heroku local

If everything looks good commit and push to git.

Then push to heroku: git push heroku master

@barriault
Copy link

Maybe add be sure Node.js and Yarn are installed to your "Before we begin" section?

@chair28980
Copy link
Author

Maybe add be sure Node.js and Yarn are installed to your "Before we begin" section?

@barriault can you explain a bit further? Did you run into an issue with this?

@barriault
Copy link

I basically had a fresh installed system and got a few warnings about this stuff being missing when I created a new Rails app. Rails 6 uses them with Webpacker to compile javascript assets instead of the asset pipeline. So I thought a quick note about them might help new users not freak out when they see the warnings. Other than that I think this gist it great.

@barriault
Copy link

And the only thing I do differently is use puma-dev instead of heroku local.

@chair28980
Copy link
Author

@barriault thank you for the feedback! I'll add your suggestion, I'm happy you found this useful 😄

@jabbett
Copy link

jabbett commented Mar 20, 2020

Say more about the Procfile stuff & heroku local ... I've never used those before. (I usually just run rails s locally...)

@chair28980
Copy link
Author

chair28980 commented Mar 22, 2020

Say more about the Procfile stuff & heroku local ... I've never used those before. (I usually just run rails s locally...)

@jabbett the Procfile modification is taken from the heroku docs https://devcenter.heroku.com/articles/getting-started-with-rails6#create-a-procfile

I think the logic is they are training users to operate "the heroku way".

@chair28980
Copy link
Author

@jabbett it appears that one benefit of running heroku local as opposed to rails server is that spring does not run with heroku local, which is nice.

@RishiHQ
Copy link

RishiHQ commented Apr 4, 2020

I'll probably figure this out with some more digging, but Heroku isn't able to detect my Gemfile, Procfile, or Rakefile, since they're all in myapp/myapp.

Folder structure:

myapp
> myapp
   > Gemfile
   > Procfile
   > Rakefile
> node_modules
> yarn.lock

Should I deploy the subdirectory myapp to Heroku? Or do I need to do some other configuration?

@chair28980
Copy link
Author

chair28980 commented Apr 4, 2020

I'll probably figure this out with some more digging, but Heroku isn't able to detect my Gemfile, Procfile, or Rakefile, since they're all in myapp/myapp.

Folder structure:

myapp
> myapp
   > Gemfile
   > Procfile
   > Rakefile
> node_modules
> yarn.lock

Should I deploy the subdirectory myapp to Heroku? Or do I need to do some other configuration?

@RishiHQ try removing the top level myapp directory. It looks like you might have first created the myapp directory, then run the rails new myapp command creating the subsequent myapp directory. Starting over might be best if you feel you've gone too deep. Let me know if you continue experiencing any issues!

@Yoni-Satat
Copy link

Thanks a lot for this recipe! Works like magic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment