Oh C.R.U.D

Shhh..It’s a Secret!

Did you know?! When you click on a link, type a URL, or submits a form, you are making a HTTP request to a specific URL on your application?!

“NO WAY!”…

”YES WAY!”

  • Session cookies expire every time you log out or navigate away from the website.
  • Website keeps track of your movement from page to page for that specific session
  • No need to authenticate yourself on every new page you visit within the web application domain.
  • The website remembers your user information and preferences for future visits.
  • Stored on your computer, while a session cookie is temporarily stored in your web browser.
  • Faster page loading, automatic login and user authentication, and access to other potential web application features.
  • Usually created on the first visit to a page after you have created an account on the site.

Connecting Controller Actions to Views for Implementing CRUD

Important stuff here!

  • use Rack::MethodOverride
  • run ApplicationController

Create

  • (Create, Read, Update, Delete) actions using Active Record.
  • Create: Cat.create
  • Read: Cat.all/Cat.find(id_number)
  • Update: Cat.update
  • Delete: Cat.destroy

Edit

edit.erb:

  • <form action=”/cats/<%= @cat.id %>” method=”post”>
  • <input id=”hidden” type=”hidden” name=”_method” value=”patch”>
  • <input type=”text” …>
  • </form>

Delete

No view page but instead is a “delete button” on the show page of a given instance. The hidden input field is how you can submit PATCH and DELETE requests via Sinatra.

  • <form method=”post” action=”/models/<%= @model.id %>”>
  • <input id=”hidden” type=”hidden” name=”_method” value=”DELETE”>
  • <input type=”submit” value=”delete”>
  • </form>

Visual Help

--

--

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