def load_list(index)
list = session[:lists][index] if index
return list if list
session[:error] = "The specified list was not found."
redirect "/lists"
end
# View a single todo list get "/lists/:id" do @list_id = params[:id].to_i @list = load_list(@list_id) erb :list , layout: :layout end
list = session[:lists][index] if index
return list if list
session[:error] = "The specified list was not found."
redirect "/lists"
end
# View a single todo list get "/lists/:id" do @list_id = params[:id].to_i @list = load_list(@list_id) erb :list , layout: :layout end
load_list method will take in an index of the todo list in the session[:lists]'s array. If there is an index provided, then find the list item in the session[:lists] array. Store the retrieve list in the list local variable. If the list is not nil/false, then return the list and terminate the method. Then store the list hash into @list instance variable.
If list is nil/false, because there is no such index in the session[:lists] array, then list will be nil. So the return function won't be executed. Then, we create a error message and store it in the session[:error] hash. Then we redirect to "/lists" which will render the session[:error] display in the layout.erb.
session[:lists] is an array = [list, list, list]
list = { name: "work", todos: [] }
Comments
Post a Comment