To-do-list application -Sinatra project

For my second project, I was required to build a web application focusing on Model-View-Controller ( MVC ) by using CRUD — Create, Read, Update and Delete. I decided to make a to-do-list application because I personally make to-do lists to help plan out my day.

First I used gem Corneal to build a Sinatra skeleton. Then I set up todolist and user model, controllers, and views for the web application. Here is my a directory tree structure of the project.

I’ve learned about CRUD and have implemented it in my code as seen below.

Create

After request get '/todolists/new’ route, we render new.erbfile. Then post request from the form and the current user can create a new instance and redirect to '/todolists/ route.

get '/todolists/new' do
redirect_if_not_logged_in
erb :"todolists/new"
end
post '/todolists' do
redirect_if_not_logged_in
todo = current_user.todolists.build(params)
todo.save
flash[:notice] = "Your list has been created!"
redirect '/todolists'
end

Read

When we request get ‘/todolists', we set todolist of the current user into instance variable@todo and render index.erb. Then requestget ‘/todolists/:id’ it will find id number and render the show.erb file.

get '/todolists' do
redirect_if_not_logged_in
@todo = current_user.todolists
erb :"todolists/index"
end
get '/todolists/:id' do
redirect_if_not_logged_in
@todo = Todolist.find(params["id"])
erb :"todolists/show"
end

Update

When we request get ‘/todolists/:id/edit', we render edit.erb file to view the page. Then after the user clicks the update button, the form will send a PUT request which it updates and saves for us.

get '/todolists/:id/edit' do
@todo = Todolist.find(params["id"])
redirect_if_not_authorized
erb :"todolists/edit"
end
put '/todolists/:id' do
@todo = Todolist.find(params["id"])
redirect_if_not_authorized
@todo.update(todo: params["todo"], date: params["date"], time: params["time"], description: params["description"])
flash[:notice] = "Your list has been updated!"
redirect "/todolists/#{@todo.id}"
end

Delete

After the user clicks on the delete button in the form, it sends a request to delete the instance that the user choose and redirects user to ‘/todolists’ route.

get '/todolists/:id' do
redirect_if_not_logged_in
@todo = Todolist.find(params["id"])
erb :"todolists/show"
end
delete '/todolists/:id' do
@todo = Todolist.find(params["id"])
redirect_if_not_authorized
@todo.destroy
flash[:alert] ="Your list has been deleted!"
redirect '/todolists'
end

Walkthrough video of To-do-list (Sinatra project)

Walkthrough video of To-do-list (Sinatra project)

If you would like to check out my web application code, here is my code.

References

  1. https://guides.rubyonrails.org/active_record_basics.html#crud-reading-and-writing-data
  2. https://github.com/thebrianemory/corneal

Software Engineering at Flatiron

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build an Interactive CLI Using TypeScript

Introduction to Canvas 2D Context

Improving error handling in React Apollo

Functional Programming Methods in Javascript

Tablecloth: a new standard library for OCaml & ReasonML

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chay McCormick

Chay McCormick

Software Engineering at Flatiron

More from Medium

CS373 Spring 2022: Audie Bethea

STATIC VS DYNAMIC LIBRARIES

Windows 11 features a QR code scanner.

Just strings and a few arrays