Table of contents
1.
Introduction
1.1.
Heroku Overview
2.
Instances and Traffic
2.1.
Domains and Naming
3.
Setting Up Heroku Account
4.
Run this Command for Windows
5.
Run this Command for Mac
6.
Deploying to Heroku
7.
Explanation
8.
Errors in Heroku
9.
On Deployment
10.
Frequently Asked Questions
10.1.
Explain Rails Migration.
10.2.
What is the difference between false and nil in Ruby?
10.3.
What are strong parameters? Explain in brief.
11.
Conclusion
Last Updated: Mar 27, 2024

Deployment in Ruby On Rails

Author Aakash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby on Rails, usually called rails, is a server-side web application development framework created in the Ruby programming language. It adheres to the MVC (model-view-controller) architecture, which offers a default framework for databases, web pages, and web services. It also adheres to web standards like JSON or XML for data transmission and HTML, CSS, and JavaScript for the user interface.

In this blog, we will deploy our applications to Heroku's cloud platform. Heroku will allow us to develop, execute, and manage our web apps on the cloud!

Heroku Overview

It should be emphasized that Heroku is not the only location to deploy; it simply happens to be the easiest for a beginning. You may also deploy to EC2 directly.

Heroku is ideal for beginning developers since it is a free and "simple" push-to-deploy solution. Their solution is built on EC2, yet it saves you a lot of time. As a result, when you DO reach the premium tiers, it will be more expensive than EC2, but it should be worthwhile for a time. The nicest feature is that you receive unlimited free high-quality hosting for new apps.

Instances and Traffic

Heroku operates by providing you with virtual "Dynos" that execute your programme. One dyno represents one instance of your application operating at the same time. That's similar to running your software on a single machine on Localhost. Many dynos are equivalent to having multiple versions of your programme operating simultaneously, allowing you to manage greater traffic. The great thing about Rails is that you can always set up extra instances of your application if you start receiving too much traffic and users have to wait for their requests to be fulfilled.

One dyno is sufficient for the majority of your applications. A single dyno can handle a lot of traffic, and Heroku provides your first one for free. Unfortunately, if you don't visit your app in a while, Heroku will "shut down" the dyno, and your app will stop running. They don't want to squander resources on thousands of applications that no one uses.

This implies that when someone visits your site for the first time it takes around 30-40 seconds to "fire up" a dyno with the app on it. So, there are two options: pay for an extra dyno, in which case Heroku will never idle any of your dynos, or set up another service to ping your application regularly (e.g. NewRelic, see below).

Heroku allows you to administer your applications via the command line (using the "Heroku CLI" set of commands) or by visiting their website and browsing around. Almost all of the features are available in both locations, which is convenient.

Domains and Naming

When you initially deploy, Heroku will assign you a random application name, something zen-like "afternoon-falls-4209." To access the app, execute $ Heroku on the command line or navigate straight to http://afternoon-falls-4209.herokuapp.com. You may alter the name to something else, such as "my-cool-app," which becomes http://my-cool-app.herokuapp.com.

Note: If you change the name of your Heroku app, you'll generally need to manually update your Git remote, so Git understands where to send your local app when you deploy to Heroku.

On Heroku, that domain name will always be yours. Obviously, in the real world, you'd want to link it to your unique domain, such as http://my cool domain.com. To begin, you'll need to acquire the domain from a registrar such as GoDaddy or IWantMyName. The domain is a fantastic tool for finding new domains.

Once you have your domain, go into your CNAME file and change the relevant entry to direct it to your herokuapp.com subdomain. What happens to mail.yourapp.com, www.yourapp.com, and calendar.yourapp.com? That file, stored at your Registrar, effectively specifies where incoming requests should be directed. These settings are simple to modify but take several hours to take effect.

Setting Up Heroku Account

Step 1: Signing up for a Heroku account.

Go to the Heroku website and sign up with the same email address you used for GitHub and Git.

Step 2: Heroku CLI

Use the email they supplied you to activate your account. We'll now install the Heroku command line to interface with the Heroku service. We'll use curl to download and run a script to install the Heroku CLI. It is normal for this script to request your Sudo password. After you've run the command below, enter it in.

Run this Command for Windows

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

Then type the Heroku version in the command prompt, which should produce something along the lines of Heroku/7.5.1 Linux-x64 node-v10.5.0.

Run this Command for Mac

brew tap heroku/brew && brew install heroku

Step 3: Add your SSH Key to Heroku

Adding your SSH key tells Heroku whose machine is issuing the instructions, similar to how GitHub utilizes SSH keys.

Enter the following into your terminal:

Heroku keys: add

Then, hit y, followed by entering. Enter the email address you used to set up your Heroku account and hit Enter. Then, enter your Heroku account password. Then, hit y and Enter to authorize Heroku to submit your public SSH key.

The terminal may display Invalid credentials supplied. Simply hit any key to launch the Heroku website on your browser. Log in using the details you used to establish your account, and the terminal will reopen, accepting your public SSH key.

Deploying to Heroku

If you've never deployed to Heroku before and this is your first time, you may skip this part. It's intended to be a useful reference for the future.

We'll go through the basics of how it will function. It is not intended to be a step-by-step instruction... Please see Heroku's "Getting Started with Rails 7. x" tutorial for further information. Heroku commands are often prefixed with either $ Heroku run or just $heroku; thus, performing a database migration on Heroku is $ Heroku run rails. DB: migrate, and the console command is $ Heroku run console.

  • Heroku CLI to be downloaded and installed. You'll very certainly need to configure your computer's SSL setup so that it can safely transfer data to and from Heroku.
  • Install Heroku's special gems - Some changes in Rails 4 broke Heroku, so they created an extremely basic gem that you'll need to add to your application.
  • Install the appropriate database gem - if you've been using SQLite3 as your development database, you'll need to set up PostgreSQL for production because it's Heroku's sole database. This entails adding the pg gem to your gemfile and populating the appropriate columns in your database.yml file.
  • Using $ Heroku create, you may build a new Heroku application from the command line. This will also add a new remote to the Git setup, so Git knows where to push your app (which you won't have to worry about).
  • Ready? Enter the command $ Git push Heroku main to push.
  • But hold on, there's more! The final step is to manually configure your database. Anytime you perform migrations or make changes to your database, remember to do them on Heroku. You'll probably execute $ Heroku run rails DB: migrate if this is your first database. If you've already set up seeds, you can run them now.

Explanation

When you built the new Heroku app, you also set up the "Heroku" remote to refer to your Heroku application. When you run $ Git push Heroku main, Git just sends your code to Heroku.

Heroku then does the same thing you do for your Localhost. It will first take the "slug" of code and files you submitted, determine your Ruby version, and then conduct a $ bundle install. It connects to your database and then starts the asset pipeline.

Rails only partly execute the asset pipeline during development, running all preprocessors but independently serving asset files such as stylesheets and javascript (check your local server logs to see it serving dozens of individual files). In production, Heroku will finish the job by not just executing the preprocessors but also mashing your assets into the single files with the timestamp names (as I type, the stylesheet is assets/application-1fc71ddbb281c144b2ee4af31cf0e308.js).

So that it does not have to perform this section of the asset pipeline (which will not change from one visit to the next), every time a new HTTP request is sent, Heroku will "precompile" the assets ahead of time and serve them from there.

Errors in Heroku

You will make mistakes. The two most common locations where problems occur are during the deployment process and when you attempt to launch your app (e.g. by getting a 500 server error). The trick, as always, is to remain cool and follow a step-by-step troubleshooting approach. It's typically a basic problem, so if you check the logs or error output, you can sort it out straight or Google the message to discover a useful Stack Overflow article.

 

You will make errors. The two most prevalent issues arise during the deployment process and while attempting to activate your app (e.g. by getting a 500 server error). The solution, as usual, is to keep calm and take a step-by-step approach to troubleshoot. It's generally a simple problem, so if you check the logs or error output, you can usually figure it out or Google the message to find a helpful Stack Overflow post.

On Deployment

You'll most likely make pretty simple mistakes the first few times. Some may be related to correctly configuring Heroku, which should be evident if the error messages say something like "we can't locate this command that you typed" or "you're not permitted to perform this."

Another typical early error is failing to include a gem (or failing to include it in the relevant portion of your gemfile - remember, we're in the production section, not the development section).

After the early errors have been avoided, another typical type of error is connected to the asset pipeline. I'm not going to pretend to understand where all of this comes from - I've encountered asset pipeline difficulties hundreds of times before, and you should also be prepared for them. Some gems and settings appear to interfere with Heroku's ability to precompile assets. When the deployment fails, or your application appears unable to locate stylesheets or images (this should be obvious if you have issues in your browser's console), you may experience an asset error.

Deployment issues, including asset precompilation failures, are frequently resolved by altering your Rails configuration files. You'll most likely need to change our config/environments/production for the two primary files.RB (the most common) and config/initializers/some gem.RB (if a gem needs to be configured). Often, the advice on Stack Overflow may urge you to add or change one of the parameters, such as config. Assets.compile = false. Please be patient.

To resolve a precompilation issue, you may be required to manually precompile the assets and then just provide the generated file to Heroku. This occasionally works... It's not a panacea, and it's inconvenient to re-run the compilation command every time you distribute changes to assets.

Check out most important Git Interview Questions here.

Frequently Asked Questions

Explain Rails Migration.

A Rails migration is a tool for modifying an application's database structure. Instead of dealing with SQL scripts, you specify database changes using a domain-specific language (DSL). Because the code is database-agnostic, you may rapidly transfer your project to a new platform. Migrations may be rolled back and maintained alongside your application source code.

What is the difference between false and nil in Ruby?

In Ruby, false is a FalseClass object representing a boolean value, whereas Nil is a NilClass object representing the lack of a value. It has a unique object id of 4.

What are strong parameters? Explain in brief.

Many Rails projects use Strong Parameters, also known as Strong Params, to improve the security of data submitted via forms. Developers can utilize strong parameters to regulate which parameters are accepted and used by the controller. Any unnecessary or possibly dangerous parameters will be disregarded and properly filtered out by permitting just the anticipated parameters.

Conclusion

This article extensively discussed how to deploy applications to a cloud platform. We hope this blog has helped you enhance your knowledge relating to Ruby on Rails.

Check out our Ruby Vs Ruby on RailsRuby on Rails for Web Dev Projects, and All about Ruby on Rails articles. Practice makes a man perfect. To practice and improve yourself for the interviews, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!

 

Live masterclass