Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

David Boureau
David Boureau

Posted on • Edited on • Originally published atalsohelp.com

How to deploy a Rails 7 app to Heroku

Article orginally published here :https://alsohelp.com/blog/how-to-deploy-a-rails-7-app-to-heroku

What is Heroku?

Heroku is the most well-known deployment platform, i.e. a place where to make your web application available to the public, on the wild Internet - so not just on your local machine.

Despite the raise of multiple competitors, for many years now, it is still unbeaten in terms of simplicity and ubiquity.

Heroku is so good, that they are able to increase prices despite the lack of roadmap - a miracle in the IT field.

I wrote aboutHatchboxIO recently, which was a decent alternative for Rails users.

The bare minimal Rails 7 application

We are going to ensure that the deployed app is able to handle JS, CSS, and relational database. I see too many tutorials where a "Hello world" page is considered good enough. I personaly advise to try a little more than that, in order to compare deployment platforms from a broader perspective : assets compilation, database, seeding, and so on.

Prerequisites for this tutorial

ruby-v# 3.3.0rails-v# 7.1.3bundle-v# 2.4.10node-v# 20.11.0yarn--version# 1.22.21git--version# 2.34.1psql--version# 14.11
Enter fullscreen modeExit fullscreen mode

Build a simple Rails app

Ok we take the most simple use case, so let's useBoring Rails™ here

mkdirmyapp&&cdmyappecho"source 'https://rubygems.org'"> Gemfileecho"gem 'rails', '7.1.3.2'">> Gemfilebundleinstallbundleexecrails new.--force--database=postgresql-j=esbuild-c=tailwind
Enter fullscreen modeExit fullscreen mode

So now we have a production-ready database (postgre), a JS builder (esbuild), a real-world framework (tailwind)

Add an authentication workflow

In order to have some logic, as well as a populated database, let's add an authentication gem.

bundle add authentication-zerobin/rails generate authenticationbin/rails db:create db:migrate
Enter fullscreen modeExit fullscreen mode

The bare minimal CSS and JS

Just add a little bit of Tailwind classes on one element to see if CSS will work properly

Insideapp/views/sessions/new.html.erb, change the form submit as follow :

<divdata-controller="hello"><%=form.submit"Sign in",class:"text-white bg-blue-700 hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300 font-medium rounded-full text-sm px-5 py-2.5 text-center me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"%></div>
Enter fullscreen modeExit fullscreen mode

And changeapp/javascript/controllers/hello_controller.js as follow :

import{Controller}from"@hotwired/stimulus"exportdefaultclassextendsController{connect(){console.log("Stimulus is working!")}}
Enter fullscreen modeExit fullscreen mode

The Procfile

The Procfile is an easy-to-understand text file, where you describe what will be run on each release

Good news : you already have a Procfile.dev file for local developments.

web:envRUBY_DEBUG_OPEN=truebin/rails serverjs: yarn build--watchcss: yarn build:css--watch
Enter fullscreen modeExit fullscreen mode

This is the local Procfile.dev - even if you don't know about it, you can guess it launches a server, and watch changes about js and css.

Now addProcfile file like this, at the root of your project :

web: bundleexecpuma-C config/puma.rbrelease: bin/rails db:migrate
Enter fullscreen modeExit fullscreen mode

Before Heroku - check that everything is working locally

Launch your app locally with

bin/dev
Enter fullscreen modeExit fullscreen mode

And open your browser at localhost:3000

You should see the blue button for "sign in", meaning that Tailwind is working locally.

You should see "Stimulus is working!" in the web dev console of the browser.

Try to login with credentials - it should show an error.

Good! So now we have something that is closer of an actual production webapp.

Deploy to Heroku

First, install theHeroku CLI on your local machine.

Then, stay at the root of "myapp" in your terminal, and type :

heroku--version# Should print current Heroku CLI versionheroku login
Enter fullscreen modeExit fullscreen mode

Now credit card is required (alas) atHeroku billing page

Then

heroku create heroku addons# Should print : No add-ons for app MyAppheroku addons:create heroku-postgresql:miniheroku buildpacks:add--index 1 heroku/rubyheroku buildpacks:add--index 2 heroku/nodejs
Enter fullscreen modeExit fullscreen mode

And finally

git add.&& git commit-m'initial commit'git push heroku main
Enter fullscreen modeExit fullscreen mode

Wait for one minute or two, you should see the build logs in your terminal...

and...

Done!

Now you should see your app under the heroku dashboard, which is pretty intuitive.

Open the app, check again that the CSS and JS are working properly.

Summary

Setting up a real-world application was actually longer than deploying an app to Heroku, which is still nowadays the best cross-deployment app for junior developer. I wouldn't advise to stick to Heroku for a long time. Once you get your first users, it may be time to consider another platform, since it's really not cheap once you meet success.

Good luck to all, health first!

David.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Rails & JS dev. Build products.
  • Location
    Nantes, France.
  • Joined

More fromDavid Boureau

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp