5beberson
4 min readJan 9, 2021

--

After enjoying a nice winter holiday break and despite Covid managing to enjoy some time with family (well, in-law family as mine is in France), it was time to start 2021 with an exciting project with Flatiron School!

In this blog, I will breakdown the steps to create a sinatra project:

  1. Create your github repo, then install the Corneal gem which will set up most of your app and bundle install. Great! Now let’s go to step 2.

2. Make sure everything is working fine by running shotgun in your terminal. In your application_controller, set your first Get request to “Hello world” and make sure it displays in your browser. If it works, you are on the right path and ready to start.

  • Pro tip 1: Open a new terminal so you can let shotgun run while having your terminal where you can use your console (we will often use tux which is great to manage your database -if you need to drop your database, open tux and type “YOUR-MODEL_NAME.destroy_all” and it will clear all your data)
  • Pro tip 2: In case you encounter an error message after running shotgun, you may have shotgun running somewhere else. Type “lsof -i tcp:9393” in your terminal and it will show you the PID where it is open. Type “kill PID-number”) and that will make the trick.

2. Having a past in the restaurant industry, including a few years as a restaurant manager, I decided to build a project that will store wines from the West Coast (California, Oregon and Colorado). This could be useful for restaurants in order to create wine lists. In order to set up my app, I watched Avi’s “Building Authentication in Sinatra” video a few times and some helpful tutorials that may come in handy, along with following our great cohort (Enoch) daily lectures. I also watched a great video to set up the layout of my website (the nav bar, footer, header, mobile nav, icons…) Be aware that it might take some time so you don’t have to do it as it is not in the requirements.

3. Ok, now that the layout is set up, we have have to create our first MVC(Models Views Controllers). I started by setting up my “wine model” by typing “corneal model new wine brand …(all attributes)” in my terminal. This set up for you a model and creates a wine table. I then had to create a controller in order to direct to app. (Note that you will have to tell your app to use your controller by typing “use YOUR-CONTROLLER-NAME_controller” in your “config.ru file”)

4. In the controller, I used all 4 RESTful routes that was part of the requirements making sure to follow the Sinatra Flow (Request -> Controller -> Query / Insert database -> redirect / render (erb line):

  • GET Routes
- index route - /wines (list of wines)
- show route - /wines/:id (showing more information about a wine)
- new route - /wines/new (displaying a form to create a new wine)
- edit route - /wines/:id/edit (displaying a form to edit a wine)
  • POST ROUTE
- create route - /wines (creating a new wine)
  • PATCH ROUTE
- update route - /wines/:id (updating a previous wine)
  • DELETE ROUTE
- delete route - /wines/:id (deleting a previous wine)

Also, don’t forget to add “use Rack::MethodOverride” in “config.ru” (a Rack module for handling browser-unsupported HTTP verbs in web applications)

5. This controller will “communicate” with the Views where you will mainly find 4 files inside your folders:

Views->Name
- index.erb
- show.erb
- new.erb
- edit.erb

Theses HTML files will allow you to render the page, this is what your users are going to see, using forms to post a method to the controller and redirect to another file.

6. Once this is all done, you will have to create your session controller, which will have all your users informations in order to sign up, login and logout. Few important steps to follow:

  • Once again, type “corneal model new user attributes” in your terminal in order to create a User model and User table (a user should always have a “:password_digest” attribute
  • add Bcrypt gem to your gemfile, make sure you add “has_secure_password” to your User model.
  • enable :sessions” and “set :session_secret, ENV[‘SESSION_SECRET’]” in your application_controller (this will set up the cookies in order to remember your user information so he doesn’t have to login every time a request is made

BOTTOM LINE:

This was a fun project to work on. This was my first time create a website and I was really proud when I finished it. There are definitely a lot of things that I wish I could have done better, I have to say that I wasn’t as organized as I should have been. At the end of the week, Enoch helped us set up Heroku (after having a lot of errors), but once it worked, I had my family back in France opened my app in their browser and I was able to show them one of my first jobs!

--

--

5beberson

Made in France, built in USA. Living the American dream