In this section we are going to setup a new rails app, examine the rails app architecture and install some dependencies.
Setup a new Rails application
Lets generate a new Rails project by running:
rails new voicey-api --api -T
Change directory to your project
Set the local ruby version to 2.5.0
rbenv local 2.5.0
Breaking down the rails new command
Rails Application Structure
Lets go over some of the important files and folders in your rails application.
The App Folder
The app folder contains the following folders:
- Channels - for websocket communication
- Controllers - Controllers for our app
- Jobs - for running recurring background tasks
- Mailers - for sending emails
- Models - Models for our data
- Views - Views for formatting data
Rails uses the Model View Controller programming pattern and we will be focusing on the Model, View and Controller folders for this tutorial.
The Config Folder
│ ├── development.rb
│ ├── production.rb
│ └── test.rb
│ ├── application_controller_renderer.rb
│ ├── backtrace_silencers.rb
│ ├── cors.rb
│ ├── filter_parameter_logging.rb
│ ├── inflections.rb
│ ├── mime_types.rb
│ └── wrap_parameters.rb
│ └── en.yml
The config folder contains the following:
- Environments - Setup for our environments (development, test, production, etc..)
- database.yml - This is where we will setup our databases
- routes.rb - This is where we will add our Restful routes to.
- application.rb - Contains setting for our rails application.
Model, View, Controller in Rails
Rails uses the MVC pattern for building web applications. The MVC pattern divides the functionality of an application into 3 logical and cooperative components.
The models, views and controllers are found in their folders under the app folder.
The Model (ActiveRecord)
The Model enables us to map a database table with a Ruby object. It can handle associations, validations, queries and more.
The Model maintains an active relationship with a library called ActiveRecord, which Rails uses to manage the relationship with the database.
The View handles the formatting and presentation of data.
In this tutorial, we are using Rails in API mode, which means we will be rending JSON instead of HTML views.
The Controller is responsible for handling requests. It directs traffic to appropriate functions which coordinates with the Model and the View to render a response.
Dependencies, Bundler & the Gemfile
Rails uses a Gem called bundler to install and manage its dependencies.
Make sure you have bundler installed by running:
sudo gem install bundler
The Gemfile is a file that lists our dependencies for our rails application.
It enables us to manage the Gems that should be installed for each enviroment(production, development, test, etc..).
Provisioning a database
Installing Postgres on macOS
We will need to setup a postgres database to use with our app. For this tutorial we will be using postgres.
Follow the instructions here for installing postgres:
Installing Postgres Link
Setup Postgres on Rails
Remove the sqlite gem from your Gemfile and add the following:
Then run the follow in your terminal:
Open the config/database.yaml file and replace the contents with this:
Make sure to replace the username and password with yours
Create our database
- Create the database with:
- Rails uses the Model View Controller architecture
- The Gemfile contains all our dependencies
- Bundler is used to install and manage our dependencies
- The database.yaml file contains information on our databases
If you have feedback on this tutorial or find any mistakes, please open issues on the
GitHub Repository or comment below.