DEV Community

Cover image for The Sinatra Project: Pizza Hub
Leigha
Leigha

Posted on

The Sinatra Project: Pizza Hub

Ahhhh- pizza! The combination of flavors and textures makes it the perfect comfort food. I must have been craving some because when it was time to choose a meaningful idea for a Collection Management System project, the choice was obvious- I would create an app using MVC and Sinatra ActiveRecord with full CRUD capabilities that would allow a user to create, read, update, and delete pizzas.

How exciting! This was where I could put everything that I have learned so far into action. Although I would love to discuss every detail of this project since I had so much fun working on it, for the sake of time I will just highlight a few things I found to be notable.

To start, I was delighted to hear about a gem called Corneal that would help me get this project going. Until this point, I had been building everything from scratch. It was really cool that when I initialized this project, Corneal took care of the basic scaffolding for my app. And since the general framework was already created, I was really able to focus on actualizing all of the ideas that I had swirling inside my imagination.

As I was building this basic project, it was impressive that the Models are quite bare with the exception of inheriting from ActiveRecord::Base and the presence of a few powerful macros. The has_many and belongs_to relationships are established here, allowing for associations between users and pizzas as well as many methods made available through ActiveRecord. I used bcrypt for password encryption, so the has_secure_password macro was also included in my User class.

In this project, I used:

  • .create to create a user or a pizza

  • .find_by_id and .all to read users or pizzas

  • .update to update a pizza

  • .destroy to delete a pizza

Stretch goal: add .update and .destroy functionality to User

It was helpful to keep in mind the flow of a Sinatra request and RESTful convention as I was building the routes within the Controllers.

Building out the Views was quite interesting- I think .erb is really neat and I have very little HTML and CSS experience. This is an area that I used up a lot of time playing around with small visual and functional details. Also, I did run into an issue regarding the use of a textarea tag vs an input tag with type="text" inside the form used to create a pizza. In the case of the input tag, the data entered by the user will populate into the form shown via the '/pizzas/edit' route, but the data within the textarea doesn't without the use of additional code. Any ideas?

Stretch goal: figure out how to get user data from textarea

Here is a look at the home page as shown when a User is logged in. The top nav bar is not shown if user not valid or logged in.

Alt Text

To achieve that, I wrapped the nav bar code with the following inside the layout.erb file:

<% if Helpers.logged_in?(session) %/>
       #html code for the nav bar 
<% end %>

and the following inside the helpers.rb file:

def self.logged_in?(session)
    !!session[:user_id]
end

Stretch goal: add full functionality for Ingredient MVC

The code below was placed inside the User model to validate the presence of and uniqueness of both the User's user name and email:

validates :user_name, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true

This project is still under construction, even though at this point I have covered the basic requirements. A User can sign up, login, create, read, update, and destroy pizzas. A User must have a unique username and email, and can only update and destroy pizzas that belong to that User.

Stretch goal: improve styling and overall user experience

Stay tuned for more updates as they happen!

Photo by pixaio from FreeImages

Top comments (0)