Ruby On Rails Rest API
Build a Ruby on Rails Rest API with an example, from setup to authentication and CRUD operations.
Ruby on Rails REST API Example
REST APIs are an incredibly important part of modern web development. They allow us to quickly and easily access data from remote web services, create complex data transactions, and link multiple systems together. Rails is a powerful web framework that provides many tools for creating robust REST APIs.
In this example, we'll create a simple API to manage a list of books. The API will provide the ability to create, read, update, and delete (CRUD) books from a database. We'll use the popular Ruby on Rails web framework to create the API. Let's get started!
Creating the Rails App
We'll begin by using the Rails command line tool to create a new Rails app. The command to do that is simply:
rails new book-api --api
This will create a new Rails app in the directory "book-api" with the --api flag enabled. This flag indicates to Rails that we want to create an API-only application. This means that the application will not generate the HTML views that are usually associated with Rails apps. Instead, it will only create a server that responds to requests with JSON data.
Creating the Book Model
Now that we have our Rails app set up, we can begin creating the models and controllers that will power our API. The first model we'll create is the Book model. This model will represent a single book in our database. To create the model, we'll use the Rails generate command:
rails generate model Book title:string author:string year:integer
This command will generate a Book model with three fields: title, author, and year. It will also create a migration file which we can use to create the database table for the model. To do this, we just need to run the following command:
rails db:migrate
Creating the Books Controller
Now that we have our Book model set up, we can create the controller that will handle requests for our API. To do this, we'll use the Rails generate command again:
rails generate controller Books
This command will generate a BooksController with an empty set of actions. We'll use this controller to respond to requests for our API. We'll need to add the routes for our controller in the config/routes.rb file. We can do this by adding the following code:
Rails.application.routes.draw do
resources :books
end
This will create the routes for our BooksController. Now we can start adding the actions that will respond to requests for our API.
Creating the API Actions
Now that we have our routes set up, we can start adding the actions that will respond to requests for our API. We'll need to add actions for each of the CRUD operations: create, read, update, and delete. Let's start with the create action.
def create
book = Book.new(book_params)
if book.save
render json: book, status: :created
else
render json: book.errors, status: :unprocessable_entity
end
end
This action will create a new book from the parameters passed in the request. If the book is successfully saved to the database, the action will respond with a JSON representation of the book and a 201 Created status code. If the book fails to save, the action will respond with a list of errors and a 422 Unprocessable Entity status code.
We can now add the other actions for our API. Each action will use the same pattern as the create action, using the appropriate CRUD operation to handle the request. Once we have all the actions added, our API will be complete and ready to use!