Mission II Complete

Angelo T
5 min readJun 4, 2021

As I had mentioned in my previous blog posts, I am still very new to the programming world. Having completed a CLI project about 6 weeks ago, I am now ready to complete my second project with the Flatiron School, which included using Sinatra to create a fully functional web application. I found this project to be extremely fun to build as we began to utilize front end programming, such as HTML and CSS.

I decided to create a virtual library where users could create a book object that would include a title, author, genre and year published. Once created, only that specific user would have access to edit or delete their own submissions, but all users would have visibility to all of the books.

submission by the user has access to edit and delete
user has visibility to all books, but can’t edit those which they didn’t create

I was able to accomplish this distinction between books created by the logged in user and those that weren’t by creating an if statement that asked if the user associated with the book object was equal to the current user, than we can display the edit and delete buttons.

Please note, when you are using a seeds.rb file and don’t associate a user id with the objects that you’re creating, you need to add another piece of code to the if statement. You can see that above where I wrote && current_user != nil.

ALL OF THE FORMS

By creating a fully functional web app, one very important skill that I had to learn was how to create forms. They can be a bit time consuming a tedious at first, but once I was able to get a hang of a couple, they became second nature.

The user is able to create a new book to the library

As you can see from the code snippet above, my labels and inputs remain very similar throughout the form creation process. However, it’s very important to make sure that at the beginning of your form, you assign a method and action. The post method will essentially submit your object creation to the database and allow visibility on your views page. The action determines where the information for the form will be visible. Here, I am saying that once the user clicks Submit, the information should be passed through to the ‘/books’ page.

LEARN SOMETHING NEW EVERYDAY

As with any new subject matter that one learns, there are always concepts that come easier than others. One concept that was challenging at first were understanding helper and private methods. These allow you to reuse code throughout your application, while keeping your code DRY. The helper method allows the programmer take a method that is getting repeated and assign it usually in the views page. See example below.

As you can see above, I am utilizing the logged_in? method to tell the program that only if the user is logged in, then they will be greeted. I can also use the current_user helper method, to display the current users username in the greeting as well.

While keeping with MVC convention (models, views, controllers), it is important to keep your controller files DRY. After writing the same code over and over again, I learned about private methods. These work similarly as helpers, but are methods that can only be used within other methods. For example, the code below says that if the user id assigned to the book, isn’t equal to the user id of the current user, then they will get redirected to the ‘/books’ page. I was writing this code quite often in my controller class, so I decided to refactor and utilize private methods.

Above, you can see that three lines of code were condensed to just one called redirect_if_not_authorized.

FINAL THOUGHTS

I really had a lot of fun with this project and it still amazes me that in only 4 months, I was able to go from nothing to being able to create a fully functional web application. From creating signups and logins, to creating user security, to creating forms for users to create and store information, I have a new found respect and appreciation for programmers. It’s a lot of tedious work and time spent creating, fixing, retooling (and fixing again), but the end result of having this product is incredibly rewarding. I’m looking forward to the next phase where we start Rails!

Cheers!

--

--

Angelo T

fashion professional turned software engineering student