Sinatra, No, not Frank

One of the things that I have to learn at Flatiron/Learn is Sinatra.

Sinatra is so cool. I have made a website that requires a login in C#/ASP.net. It was quite a while ago, before I understood the benefits of using smaller methods/functions to make code more readable and DRY (Don't Repeat Yourself). The concept sounds easy, but sometimes it seems easier to repeat yourself rather than figure out how to make the code work in multiple spots. It definitely makes more sense to DRY program though. In the end you spend less time editing and figuring out where there may be bugs because everything is in one spot.

Ok back to Sinatra and why I'm using it. Sinatra is a stepping stone in the learning process to Rails. In Sinatra, your links to different pages are called routes. For example, if you want a user to go to the login page if they haven't logged in, you can create a route that looks like this:

get '/login'

   erb: index

end

If you want the user to go to the xyz page to view some information about something on that page, you just swap out '/login' for '/xyz'.

If you want a form to submit and send information to the database you can use a POST instead of GET.

you create your form, telling the form that it is a post request instead of get and then you say what route you want to use.  For example:

<form method="POST" action="/feedings/<%=@feeding.id%>">

This if from my Sinatra project. The route that is taken is a POST route. It allows you to manipulate data. I can get a variety of information from the user and then display it back to them any way that I want. I can track it using a database and then query the database using ActiveRecord.

My Sinatra project is called Feeding Tracker. You can find it on github here:
https://github.com/jmb521/feeding-tracker
Here is the video walkthru
https://youtu.be/EmsZgAQBImE

I wanted to create something that someone else could find useful. This is what I came up with. Feeding Tracker allows someone to log into their account and enter feeding information for their baby. They can create as many children as necessary to keep track of. They can edit and or delete the child information as well as add, edit or delete the feeding times.

As I come to the end of the project, I will say that one of the things that I struggled with was what had to be plural and what could be singular and I really do suck at coming up with names, so there is some redundancy within my code when it comes to naming everything. That's something I can definitely fix later though. My first goal was to get it working, then get it functional (some would call that the same thing)...but I'm talking about not just having routes that go from point a to point b, but rather a basic site menu that allows you to go from point c to point a without having to use the back button. That was probably one of the easiest things to figure out. The hardest part, was how to get 2 objects to work together so that the join tables I created worked. I had to have multiple relationships between my models. The parents (aka user) account had a 1 to many relationship with children. Children had a many to 1 relationship with parent. Feedings had a many to 1 relationship with Children and Children had a 1 to many relationship with Feedings. I had to setup 2 join tables, 1 to work with connecting Parents to Children and one to connect Children to Feedings. I have noticed that I struggle in programming of thinking too deep. What I mean by that is that I try and program in a way that is more difficult. It would be like driving around a 2 block radius only to realize that I could have cut through an alleyway to get where I wanted to go. Sinatra, ActiveRecord and Rake do a lot of that thinking for me, which is great, but I have to remember that I don't have to connect everything in the database manually.

Comments

Popular Posts