A Sinatra Story- by “Your Coding Esty”

I didn’t realize how much I depend on Software Engineering until recently.

Especially starting this Sinatra Project. Having to use M-V-C to build the web application I have been working on. M-V-C short for Model-View-Controller is a type of framework for web apps. Each file has their own job which if you think about it, keeps things pretty organized. However, things can go a bit side ways real quick if you have multiple models. We need software framework to help support the development of web apps, services, resources and websites.

My story begins with Sinatra. A Rack-based, Domain Specific Language implemented in Ruby that’s used for writing web applications allowing it to be utilized in Rails. Top companies like Apple, Heroku, National Security Agency and more use Sinatra.

The good thing is that we will now have access to pre-written methods. The result is dynamic Ruby web applications.

Requiring Sinatra in my app will give me access to methods like: get and post. Why is this so important you ask? Well, as I found out it gives a Ruby application the ability to change so it can respond to HTTP requests. Here you go. gem install sinatra You’re Welcome!

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?!

”YES WAY!”

We have Cookies!…

No, not Chocolate Chip ! “Angela” haha!

Important thing to note here is that there are two different kinds of cookies! Session & Persistent cookies.

Session Cookies :

  • 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.

Without cookies we would loose everything we added into our online cart as soon as we went to a different page…

See…now you know we need them cookies…just not the calories!

Persistent cookies:

  • 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.

So, when you talk about the quality of a users experience. Persistent Cookies are preferred.

All this leads to CRUD Stuff that still has me confused but hey I’ll get there eventually

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>

No view page but instead is a “delete button” on the show page of a given instance.

-This “delete button”, however, is a form! The form should send a DELETE request to delete '/models/:id'

-Only needs a "submit" button with a value of "delete". To appear as only a button to the user. Here's an example:

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

The hidden input field is important to note here. This is how you can submit PATCH and DELETE requests via Sinatra.

Visual Help

In my opinion I like Sinatra…C.R.U.D and it’s trickery.