Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Cleaning Up Our Code

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Code repeated inside the TodoItems Controller in the Teacher's Notes

In the Teacher's Notes of the "Cleaning Up Our Code" video, the code

before_action :find_todo_list 

is written twice on the same line in this code

class TodoItemsController < ApplicationController
  before_action :find_todo_list before_action :find_todo_list

  ...

  def find_todo_list
   @todo_list = TodoList.find(params[:todo_list_id])
  end

Is that an error or not? And what does the code do?

1 Answer

Philip Bessa
Philip Bessa
5,396 Points

It shouldn't be written twice. It's just another problem among a litany of others in this course. So much for cleaning it up, eh? LOL

before_action basically means what it says: before any method (or action) in the Items Controller, it executes the method find_todo_list first. So if it runs the create method, it first runs the find_todo_list method first. If it runs the update method, it runs find_todo_list first as well. So instead of repeating the same line of code it's just said once, with the same result.

kabir k
kabir k
Courses Plus Student 18,036 Points

Right, so much for cleaning up lol

Anyway.....thanks, Philip.