Sinatra MVC Project: The GalleryProject

Melissa Guachun
3 min readApr 25, 2021

As Phase 2 came to a close, my class was confronted with another project mode and assessment. To prevent any more loss of sleep from the deadline looming over my head I decided to jump at opportunity of thoroughly sketching my concept out for our database driven project.

I tried applying what I learned from my first project which was to make something you find personally interesting hence, the GalleryProject. Being quarantined for more than a year has made my world small and my imagination smaller. I missed going into the city and visiting the museums I grew up around. I imagined having a place of my own to collect my favorite pieces of art to admire or reference for inspiration.

When making the GalleryProject, each user is able to log their favorite pieces of art and store them in an individual and private collection. To accomplish this, I created two models- collection and user. The collection model fulfills the belongs_to relationship, belonging to the user. The foreign key belongs to the object that belongs to another object. The user model fulfills the has_many relationship, where a user has many collections. Setting up these models allows a user to sign up and have their information saved with their own attributes along with their collections.

Information pertaining to the users and their collections are saved according to the attributes of their respective databases. For example, collections is a table that is saved with an artist name, title, year made, movement, location, medium, and user_id.

create_table "collections", force: :cascade do |t|t.string "artist"t.string "title"t.integer "year"t.string "movement"t.string "location"t.string "medium"t.integer "user_id"end

These attributes enable the app to accumulate the information needed from the user and stores their information to show it to the user whenever asked.

To make the database operate with my website, I used the CRUD layout (Create, Read, Update, Delete) to create routes. The Create route was established to ask for the specific attributes (from the table above) and connect it to the collections model. The information would be saved into the database and given an id which would be stored into the user. The Read route displays the information they are receiving from the browser by matching the collection of the user with the special id given to the user from the params. For the Update route, I used user authenticator so that only the authorized user an edit or delete items from their collection.

patch "/collections/:id" do@collection = Collection.find(params["id"])redirect_if_not_authorized@collection.update(params["collection"])redirect "/collections/#{@collection.id}"end

If a user wants to edit a piece in their collection, the app makes sure that the id (from the params) of the user matches the model. This is is made into an instance variable. If the collection isn’t matched with the id of the user, then the current user is redirected to the login page. This prevents outside users from having access to information belonging to signed in/logged in users. If the id does match the collection, the browser window displays collections with the user’s collection id. The browser then retrieves the information of the user to be updated.

Finally, if a user want’s to Delete an item from their collection, they’re able once they get through user validation as seen in the Update block. Once they’ve through user validation, they are able to destroy any or all pieces in their art collection and then redirected to the index view of the collection.

When creating the CRUD layout, I found creating private helper methods to be useful when implementing user authentications.

privatedef redirect_if_not_authorizedif @collection.user != current_userredirect "/login"endend

It helped me sorta cut corners and streamline my code. I wanted to make sure that the user operating the site matched the same user that belongs to the collection. If not, the user is redirected to the log in, otherwise, the user is able to operate all the functionality of the app.

--

--

Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.