Here we are at project #2 and I can hardly contain my excitement for finally learning how to build a website! For my project I made a web app that will allow users to create lists where they can store movie titles. The app will allow them to create, read, update, and delete (C.R.U.D.) items they add to their user profile within the app. At first this project was hard to comprehend but in the end it all started making sense. Shoutout to the former Flatiron student that made the Corneal gem. You saved me a headache.
After running the Corneal gem and setting the structure for my app I started on my migration folder by creating my tables for users, lists, and movies.
Next would be the models. Each user would have their lists and each list would have it's movies. This would require me to give the users and lists models a has_many relation. Movies and lists would go on to have a belongs_to relation. While adding the realtions I also added validations that would check to see if user input is valid.
When I finished the models I moved on to the controllers. There is the application controller that inherits from Sinatra and a users and lists controller that inherit from the application controller. The user and lists controller are made for the purpose of creating CRUD actions while the application controller is used for giving access to all of the methods like GET and POST.
Once the controllers were finished I started on the routes for each page.
Create: Done through a GET route and stores the information with a POST route.
Read: Done by adding another GET route which I could use to list all movie lists that belong to a specific user. This is possible because of the activerecord methods has_many and belongs_to that were used in the lit and user models.
Update: Done by using a GET route and adding logic to find the list by it's ID then using extended ruby to add new changes to the list name.
Delete: Delete is done by simply finding the object that you are searching for and using the destroy method from activerecord.
After my routes were done I started on my views to lay out the pages to communicate what options were available to the user. Sinatra uses ERB (extended ruby) which allows us to write html as if it were an html file. Inside of the erb files is where I wrote the form tags that contain code that would would send information to the databases I created back when I made my tables.
Post method: Allowed me to save info to a table within the database.
Patch method: Allowed me to update information within a table.
Since CSS wasn't listed as a requirement I simply left everything the way it was when I used the Corneal gem. This includes the HTML that was included as well.
Top comments (0)