Creating ToDo Items with POST Requests in Ruby on Rails

Introduction

Welcome back! In this lesson, we will explore how to set up a POST request in a Ruby on Rails ToDo application to create new ToDo items. This marks an essential step in building our RESTful API, enabling users to add new tasks to their ToDo lists.

By the end of this lesson, you will be able to create a new ToDo item using a POST request. This skill will allow your application to handle new data input from users, making it interactive and dynamic. Let's get started!

Creating the TodosController

The TodosController is responsible for handling requests related to ToDo items. In this lesson, we will focus on setting up the create action within this controller to enable the creation of new ToDo items.

Below is the code required to set up the TodosController:

Ruby
# app/controllers/todos_controller.rb
class TodosController < ApplicationController
  def create
    todo = TodoService.create(todo_params)
    render json: todo, status: :created
  end

  private

  def todo_params
    params.require(:todo).permit(:title, :description)
  end
end

Explanation:

  • create action: This action receives the incoming POST request, invokes the TodoService.create method to handle the creation of a new ToDo item, and then renders a JSON response with the created item and a created status. As mentioned previously, Rails automatically links the POST request on /todos to the create action in the TodosController.
  • todo_params: This private method ensures only the permitted parameters (title and description) are passed to the model, protecting against unwanted data. In Rails, such strong parameters help prevent unwanted data from entering our database by ensuring only allowed attributes are passed through the controller actions. This provides a layer of security and data integrity:
    • params.require(:todo): Ensures that the parameters sent with the request contains a todo object.
    • permit(:title, :description): Specifies the attributes that are allowed within the todo object, filtering out any other input.

By using strong parameters, we enforce strict criteria on incoming data, enhancing the security and reliability of our application.

Connecting with TodoService

To keep our code modular and maintainable, we delegate the business logic for creating ToDo items to the service class TodoService. This separation of concerns is a standard best practice in Rails applications.

Here is how we implement the TodoService.create method:

Ruby
# app/services/todo_service.rb
class TodoService
  def self.create(params)
    todo = { id: @todos.size + 1, **todo_params }
    @todos << todo
    todo
  end
end

Explanation:

  • todo = { id: @todos.size + 1, **todo_params }: This line creates a new ToDo item with a unique ID based on the current size of the @todos array and the provided parameters.
  • @todos << todo: The newly created ToDo item is added to the @todos array.
  • todo: The new ToDo item is returned.

This approach makes our TodosController more concise and focused purely on handling HTTP requests and responses.

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