Creating a simple Ruby on Rails API
If you're looking to build a Rails API from scratch, this article will guide you through the process and highlight some of the most common configurations used in Rails API development.
Getting Started with Rails API
The first step is to generate a new Rails API application using the rails new
command. However, you'll need to include the --api
flag at the end of the command to ensure that your application is configured for API-specific features.
To get started, navigate to your project directory and run the following command in your terminal:
$ rails new library --api
This command generates a new Rails application named "library" with additional API-specific configurations. It also skips the creation of .erb files with ActionView.
Once the command finishes, navigate to the "library" directory and open it in your preferred code editor. You're now ready to generate code for handling API requests.
Using Rails Resource Generator
For this application, we would like for our library app to display the list of books only. And for that feature, we would like our app to handle a GET request to /books. 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 the Book
model, we want a table with the following attributes:
Column Name | Data Type |
title | string |
author | string |
publication_year | integer |
Instead of creating routes, models, controllers, and migrations separately, you can use the resource
generator that comes with Rails to generate all of them at once. Pretty cool, right? Navigate to your terminal and run the below code:
$ rails g resource Book title author publication_year:integer
This command generates the following:
A migration for creating a
books
table with the specified attributes.A
Book
model file and aBooksController
controller file.resources :books
added to theroutes.rb
file.
Once you've generated the code, you'll need to run the migration by running the following command:
$ rails db:migrate
Seeding the Database with Sample Data
To test your application, you can use sample seed data. Copy the following data into the db/seeds.rb
file:
Book.create([
{
title: "Dark Vader",
author: "Jevans Otieno",
publication_year: 2022
},
{
title: "The Wanderer",
author: "John Doe",
publication_year: 2021
},
{
title: "In a Little While",
author: "Jane Doe",
publication_year: 2020
}
])
To seed the database, run the following command in your terminal:
$ rails db:seed
Creating Routes for Your API
For this article, we'll focus on the index
action. You can read more about the different actions the resource
generator generates in the official Rails documentation.
Navigate to the config/routes.rb
file and update it to the following code:
resources :books, only: [:index]
With the code in place, you can now update the index
action in your BooksController
with the following code:
def index
books = Book.all
render json: books
end
Starting the Rails Server
Finally, run the Rails server by running the following command in your terminal:
$ rails s
Visit http://localhost:3000/books
in your browser to view the JSON response with your book data.
Conclusion
Congratulations, you've now created a simple library API using Rails! You can learn more about the Rails command line in the official Rails documentation, and continue to explore the world of Rails API development. Happy coding!