Sinatra and ActiveRecord

Fay Vera
2 min readMar 5, 2021

--

For my Sinatra Project I built a Study Hall Application where students can Enroll in different courses and share resources. I used Sinatra and ActiveRecord together to create it, building a MVC Sinatra application.

ActiveRecord takes care of the M (Model), which is the logic of the application, handling the data. The V (Views) are what the user sees, so it uses HTML, CSS, ERB in order to build it. The C (Controller) manages the in-between transactions in between the two.

ERB (Embedded Ruby) is essentially HTML with Ruby combined. It was so interesting working with it for the first time, and to also be able to style it with CSS.

Here is a snippet from my “course_show” view. What this page does is shows the user a specific course of their choice. To make it dynamic, I am not hardcoding each course (also because students can add courses if they want, so you can see it could become outdated really fast).

I’m getting information from the database through instance methods, so I am not accessing the database straight from the view. This instance method is an object of the class ‘Course’, where I found through a find_by method in the controller.

I then lay out some information from that course for the user, and if they are not enrolled in it yet, I give them the option to enroll. If they are, I give them the option to unenroll. For these actions, I am using forms that lead to a “POST” controller.

When they enroll
When they unenroll

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response