Setting Up Modification Queries - Update, Delete

Adding and Editing ToDo Items

Welcome back! In the previous lessons, you learned how to retrieve and create ToDo items. Now, it's time to make our application even more powerful by adding the ability to update and delete ToDo items. These actions are crucial because they allow users to manage their tasks dynamically, keeping their to-do list accurate and up-to-date.

What You'll Learn

In this lesson, you will:

  1. Add functionality to update an existing ToDo item.
  2. Implement the ability to delete ToDo items.

Both of these features are critical for a fully functional ToDo application. Imagine having a list that you can't edit or update — you would soon find it outdated and not very useful.

Update Functionality

Let's first look at how to add the update feature. Open your todos_controller.rb and add the following methods:

class TodosController < ApplicationController
  def edit
    @todo = TodoService.get_by_id(params[:id])
  end

  def update
    todo = TodoService.update(params[:id], todo_params)
    if todo
      redirect_to todo_path(todo[:id])
    else
      render :edit, status: :unprocessable_entity
    end
  end

  private

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

Additionally, update the TodoService to include the update method:

class TodoService
  # Existing methods...

  def self.update(id, params)
    todo = Todo.find_by(id: id)
    return false unless todo

    if todo.update(params)
      todo
    else
      false
    end
  end
end

Explanation:

  • edit method: This method retrieves the ToDo item that needs to be edited. It does so by calling TodoService.get_by_id, passing in the id from the parameters, and assigns it to the instance variable @todo.

  • update method: This method updates the ToDo item. It calls TodoService.update, providing the id and the permitted parameters from todo_params. If the update is successful (todo is truthy), it redirects to the show page of the updated ToDo. If the update fails, it re-renders the edit form, returning an unprocessable_entity status.

  • todo_params method: This private method uses strong parameters to control which attributes can be updated. It requires the parameters to have a todo object and permits only the title and description attributes.

  • TodoService.update method: Finds a ToDo item by its id and updates it with the provided parameters. It returns the updated todo object if successful, otherwise false.

In the form view (app/views/todos/edit.html.erb), we'll create a form for users to edit their existing ToDo items:

<h1>Edit Todo</h1>
<%= form_with scope: :todo, url: todo_path(@todo[:id]), method: :put, local: true do |form| %>
  <div>
    <%= form.label :title %>
    <%= form.text_field :title, value: @todo[:title] %>
  </div>
  <div>
    <%= form.label :description %>
    <%= form.text_area :description, value: @todo[:description] %>
  </div>
  <div>
    <%= form.submit "Update" %>
  </div>
<% end %>
<%= link_to 'Delete', todo_path(@todo[:id]), method: :delete, data: { confirm: 'Are you sure?' } %>

Explanation:

  • form_with: Generates a form for the ToDo item using form_with. It sets the method to put to represent an update action and scopes it to the todo object.

  • Form elements: The form contains a labeled text field for the title and a text area for the description, pre-filled with the current values using @todo.

  • Submit button: The form includes a submit button labeled "Update" that will send the updated data when clicked.

  • Delete link: A link is provided to allow users to delete the ToDo item directly from the edit page, with a JavaScript confirmation dialog.

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