Setting Up GET Queries - Get All and Get By ID

Introduction

Welcome to the first lesson of the "Using REST API to Build ToDo App with Ruby on Rails" course. In this lesson, we will learn how to set up GET queries to fetch all Todos and fetch a specific Todo by ID. These fundamental actions are crucial for interacting with and displaying the data in our ToDo application.

In a RESTful API, GET requests are essential as they enable the retrieval of resources from a server. By the end of this lesson, you'll be equipped to handle these GET requests to display all todo items or a selected single item.

Recap of Prior Setup

Before diving into the specific functionalities of GET queries, let’s briefly revisit the existing setup, specifically the routes defined for our ToDo app.

Here’s a quick look at how our routes are set up to direct REST requests to the appropriate controller actions:

Ruby
Rails.application.routes.draw do
  resources :todos
end

This code sets up common REST routes for the todos resource, which will trigger the corresponding methods in the TodosController.

Naming Conventions and Path Parameters in Rails

In Ruby on Rails (RoR), naming conventions and path parameters are crucial for setting up RESTful routes and ensuring the corresponding controller actions are correctly mapped.

Naming conventions in Rails follow standard REST principles. For example, GET /todos maps to the index action in the TodosController to retrieve all todos, while GET /todos/:id maps to the show action to retrieve a specific todo by its ID. The resources :todos declaration in the routes.rb file automatically sets up these routes and others, adhering to RESTful conventions:

  • Index: Retrieves all resources, mapped to GET /todos.
  • Show: Retrieves a single resource by ID, mapped to GET /todos/:id.
  • Create: Creates a new resource, mapped to POST /todos.
  • Update: Updates an existing resource by ID, mapped to PUT /todos/:id.
  • Destroy: Deletes an existing resource by ID, mapped to DELETE /todos/:id.

Path parameters in Rails are defined within the route itself using a colon (:) followed by the parameter name, such as :id. When a request hits a route with a path parameter, like GET /todos/1, Rails captures the value in the URL and makes it available through the params hash within the relevant controller action. For instance, params[:id] in the show action captures the ID segment from the URL, allowing the action to fetch the corresponding todo item from the data store. The params hash is a versatile structure in Rails that stores parameter values from the URL, form data, and other request parameters, making them easily accessible within controller actions.

The integration of these conventions and parameters ensures a clean and predictable routing mechanism in Rails applications, making the codebase easier to understand and maintain. It also facilitates a clear separation of concerns, as routes map neatly to their respective controller actions based on standard conventions.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal