Codementor Events

Create a Ruby on Rails app in just 5 steps

Published Nov 06, 2020Last updated May 04, 2021
Create a Ruby on Rails app in just 5 steps

Have you ever though about creating a Ruby on Rails application?
Maybe you have tried to follow the official guides, but were struct with the huge volume of information presented to you?

Follow me through this article and in the end you will have built your first Ruby on Rails application.

You are going to build a page that displays a random joke, fetched from official joke API.
joke_of_the_day.gif

Before you start

There are a couple of things that need to be setup on your computer in order to
work with Ruby on Rails applications.

Please, refer to this guide to install Ruby on Rails.

You can skip the part about installing and configuring Git, setting up PostgreSQL/MySQL databases (we will stick with the default database - SQLite).

Install SQLite

On macOS run the command:

brew install sqlite3

On Windows no extra steps are needed.

On Linux (Debian based) run the following:

sudo apt-get update
sudo apt-get install sqlite3

On Linux (RPM based) run the following:

sudo yum update
sudo yum install sqlite

Once you have reached the part of the guide where by entering
rails -v
you get a version number like 6.0.0 you are done with the setup and can ignore everything after that.

Now you have reached the fun part: time to actually create
your first Ruby on Rails application!

Creating the application

There are two ways that you can create a new Ruby on Rails applications:

  • by creating it from scratch (writing each file yourself)
  • by using rails new command

Use the easiest and fastest one: the rails new command.

Using the rails new generator

You want to run a particular kind of rails new command, where some of the features that you don't need at this time are disabled.

So go ahead and run:

rails new my_first_rails_app --skip-turbolinks

Turbolinks is a something that you do not need to create this application as it is important to keep this process as simple as possible.

Starting the Rails server

Once this command finishes, all you have to do is to run
rails server
to start the development server and visit
http://localhost:3000
on your web browser.

If you see a page like this
rails_application.png
then everything appears to be working as expected.

Starting the Webpack Development Server

To prepare the application for working with JavaScript you must start the Webpack
Development Server by opening a new terminal tab and running:
bin/webpack-dev-server

Generating the Joke controller

Now you want to be able to visit http://localhost:3000/joke
and see a random joke that has been fetched by using the API (jokes api url).

For that to happen we have to create a new

  • route
  • controller
  • view

To create a new controller and route we can use the
rails generate controller Joke show
command.

The Joke part means that we want to generate the joke controller and the show part means that the controller should be able to handle only GET requests.

The files that were created/modified and are important to us at this time are:

  • config/routes.rb
  • app/controllers/joke_controller.rb

This command will edit the config/routes.rb file to link the request you make to
code that will prepare data and determine which view should be rendered.

But you have to do a slight modifications to the code in the config/routes.rb.

If you open the file you should see something like this:
get 'joke/show'

You have to edit that line to match the code below:
get 'joke', to: 'joke#show'

What this does is allows you to make a GET request to /joke and the application will execute the show method of JokeController class within app/controller/joke_controller.rb.

The basic format for this kind of route is:

get '<route>', to: '<controller_name>#<action_name>'

An action is basically any public method that is inside of the controller class.

Check out how routes work in the routing documentation.

For now you can think of controller as the place where the config/routes.rb file
will link to when processing the request and where it is determined which View
(or page, in other words) to render.

The fact that the show method in the JokeController is empty means that the default file resolution path is going to be used - the app/views/joke/show.html.erb will be rendered.

Check out how controllers work in the controller documentation.

HTML code

Now if you visit http://localhost:3000/joke you should see a page that looks like this:
initial_joke_show_page.png

What you want to do is to modify the view code for this page.
Open the file path that is displayed on the page:
app/views/joke/show.html.erb

If you take a closer look you will notice that the file ends in a .erb extension.
That stands for embedded Ruby.
It allows you to execute Ruby code while creating the resulting HTML file.
But for the purposes of this application you can treat this as a regular HTML file.

Read more on ERB here.

So change the file contents to be:

<h1>Joke of the day</h1>
<p data-joke-container="true"></p>

The joke will be fetched from official joke API and placed in the <p> tag with the data attribute by JS code that we will write.

JavaScript file

Now you have to create the final part of our application - JavaScript file where the fetching and displaying of the joke is handled.

Create a new file app/javascript/home.js and place the following content inside:

document.addEventListener('DOMContentLoaded', function() {
  const apiUrl = 'https://official-joke-api.appspot.com/random_joke';
  const jokeContainer = document.querySelector('[data-joke-container]');

  fetch(apiUrl).
    then(response => response.json()).
    then(data => {
      jokeContainer.innerHTML = data.setup + ' ' + data.punchline;
    });
});

Here we execute a function when the page content has loaded.
Inside this function we set the variable for the URL of the JOKE API as
well as store the joke container DOM element in a variable.

Then we make a GET request to the joke API, transform the received response to JSON and set the innerHMTL attribute of the to the setup and punchline of the joke separated by a whitespace.

Refresh the page and enjoy a new joke!

Conclusion

In this post I showed you how to set up your computer for running Ruby on Rails applications as well as how to use the rails generate command to create controllers and other associated files in a breeze.

You took a glance at how the routes are configured to execute the controller code,
which in turn (by default) rendered a view.

You also modified the view to include a <p> tag where the joke will be displayed
and added a JavaScript file where joke fetching and displaying was handled.

Congrats on building your first Ruby on Rails application!

Discover and read more posts from Kristians Kuhta
get started
post commentsBe the first to share your opinion
Mohamed masthan kani
8 months ago

Super app

Show more replies