Creating a Rails API from Scratch
Learning Goals
- Use the
--apiflag to create an API-only Rails app - Use the
resourcegenerator
Introduction
We've spent a lot of time now focusing on the backend, and now's a great opportunity to see what we can actually do with all the power of a Rails API to support a frontend application as well.
Throughout this section, we'll be building a DVD shop. We'll have a Rails API to support a React frontend application, and we'll be focusing on how that client-server communication process works, as well as some challenges involved in communicating between two separate applications.
In this lesson, we'll start by building the Rails backend from scratch and talk through some of the typical configuration when creating a new Rails API.
Generating a Rails API
Just like we saw at the beginning of the phase, we can use rails new to
generate a new Rails application. We'll run that same command with a few
additional options to optimize our Rails app. Let's generate the backend code
for our dvd-shop. Use cd .. to navigate out of the lab directory, and run:
$ rails new dvd-shop --api --minimal--api: this flag will create our new application with some additional API-specific configuration, and will skip the code for generating.erbfiles with ActionView.--minimal: this flag skips a lot of additional Rails features that we won't use in our API, such as code for sending emails and processing images. Read more about the--minimalflag.
The reason we ask you to
cdout of the lab directory is because when you generate a new Rails project, it will automatically create a new Git repository for your Rails project. Since the lab directory is already a Git repository, it's better to create this new project in its own directory, so you don't end up with nested Git repositories.
With that code in place, let's generate the code for handling our first request from the client.
Using the Resource Generator
One of the main features of our frontend application will be to display a list
of movies. For that feature, we'll want our API to handle a GET request to
/movies.
To get that request working, we'll need to create a route and controller action on our Rails server. We'll also need a model to interact with the database, and a migration to generate the corresponding database table for this model.
For our Movie model, we'll want a table with the following attributes:
| Column Name | Data Type |
|---|---|
| title | string |
| year | integer |
| length | integer |
| description | string |
| poster_url | string |
| category | string |
| discount | boolean |
| female_director | boolean |
We could create the route, model, controller, and migration individually, but
since this kind of operation is pretty common for a Rails developer, there's a
handy generator that will set up all the code we need: rails g resource.
Navigate into the dvd-shop directory and run this code in your terminal:
$ rails g resource Movie title year:integer length:integer director description poster_url category discount:boolean female_director:boolean --no-test-frameworkThis command will:
- Generate a migration for creating a
moviestable with the specified attributes - Generate a
Moviemodel file - Generate a
MoviesControllercontroller file - Add
resources :moviesto theroutes.rbfile
It's a powerful command, so make sure to use it sparingly! You should only use
rails g resource if you truly need all of that code generated.
Running the API
To get some sample data into our application, we've provided a seeds.rb file
in the root directory of this repo. Copy the contents of this file into your
db/seeds.rb file. Then, to set up and seed the database, run:
$ rails db:migrate db:seedLet's update our routes.rb file to set up just the one route our frontend
needs, for the time being:
# config/routes.rb
resources :movies, only: [:index]We can also add the index action to our controller:
def index
movies = Movie.all
render json: movies
endWith that code in place, run rails s to start the server, and visit
http://localhost:3000/movies in the browser to see our movie data. Success!
Conclusion
When creating a new Rails API project from scratch, you can use the --api flag
to have Rails optimize your project for building a web API.
We also saw how to use the resource generator, which can help quickly set
up the code we need to create RESTful routes and CRUD functionality for one
single resource.