My Experience Building a Sinatra Content Management System
Tags: project, cms, sinatra, flatiron
Published: 2020-12-13 23:30:41

Creating my own web application using Sinatra was a challenging yet gratifying experience.
 
To begin with, getting the application off the ground and working was not as easy as I had expected. I had thought that getting a database configured and working with ActiveRecord would have only taken me a few minutes. However, I ran into one big problem. Any time I tried to create a migration I would get an error that ActiveRecord was not established with a database. After a couple hours of googling and trying many combinations of different things I finally found out that all I needed was a 'require sinatra' statement at the top of my environment.rb file. I'm still not entirely sure why this was the case since previous lessons that I worked through never needed to require sinatra but I was happy to finally solve that problem. 

Following that, I was relieved to find out that creating tables and connecting them to my models did not provide any errors. Now I was able to move on to the real meat and potatoes of my project.

Creating views and controller routes was straightforward and enjoyable. I was able to get most of the functionality of my web app built without a hitch. However, I ran into an interesting dilemma when I was creating a form for making a new recipe. I wanted the user to be able to specify any number of ingredients and directions to a recipe. Moreover, I also wanted the user to be able to designate sub-categories for their recipe, such as differentiating ingredients for the cookie and ingredients for the frosting in a sugar cookie recipe. However, without any knowledge of javascript, this proved to be quite difficult. My workaround for this problem was to provide the user with a textarea in which they would delineate directions and ingredients by using numbers. Such as, 1. sugar, 2. flour, 3. etc. They could also designate sub-categories by using words wrapped with star symbols. Then I built a Helper method that takes the long string with all ingredients or directions and returns a hash with keys for each sub-category and the values being arrays with each direction and ingredient that belong to that sub-category. Ingredients and Directions that don't belong to a sub-category get attributed as belonging to the main recipe. Coming up with this solution and writing the code to make it work was the most challenging part of this project for me. Although having some knowledge of javascript probably would have made this much easier, it wouldn't have been as fun.

Once I was done creating all the views and routes I moved on to the last steps which was seeding example recipes and styling. To seed data, I used my webscraper which I had built for my previous project and tweaked it so that it would find Christmas cookie recipes and instantiate new recipe objects. Although I could have just copy and pasted recipes from the internet as examples I felt like it would be nicer to have the ability to seed different kinds of cookie recipes if I ever wanted. Finally, I added a bit of styling to my web page to spruce up the plain black and white layout. Styling isn't my passion so I didn't spend too much time on that. 

After everything was said and done I felt pleased that I had built a fully functional web application. Although its far from being as nice looking and user friendly as other larger websites, It's still gratifying to know that I was able to build it on my own and I'm looking forward to being able to build more complex web apps in the future.