Structuring Our Ruby App

To begin with, we’ll take the modular approach with this build so it’s easy to organize functionality in a clean and intuitive way.

Our cost-of-living calculator app needs:

  • A root page, which will act as our landing page.
  • Another page with a form where a user can input their salary information.
  • Finally, a results page that displays some living expenses for the chosen city.

The app will fetch cost-of-living data from an API hosted on RapidAPI.

We won’t include any tests or user authentication to keep this tutorial brief.

Go ahead and create a folder structure like the one shown below:

bash

. ├── app.rb ├── config │   └── database.yml ├── config.ru ├── db │   └── development.sqlite3 ├── .env ├── Gemfile ├── Gemfile.lock ├── .gitignore ├── lib │   └── user.rb ├── public │   └── css │   ├── bulma.min.css │   └── style.css ├── Rakefile ├── README.md ├── views │   ├── index.erb │   ├── layout.erb │   ├── navbar.erb │   ├── results.erb │   └── start.erb

Here’s what each part does in a nutshell (we’ll dig into the details as we proceed with the app build):

  • app.rb – This is the main file in our modular app. In here, we define the app’s functionality.
  • Gemfile – Just like the Gemfile in a Rails app, you define your app’s gem dependencies in this file.
  • Rakefile – Rake task definitions are defined here.
  • config.ru – For modular Sinatra apps, you need a Rack configuration file that defines how your app will run.
  • Views folder – Your app’s layout and view files go into this folder.
  • Public folder – Files that don’t change much — such as stylesheets, images, and Javascript files — are best kept here.
  • Lib folder – In here, you can have model files and things like specialized helper files.
  • DB folder – Database migration files and the seeds.rb will go in here.
  • Config folder – Different configurations can go into this folder: for example, database settings.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *