Render? Redirect? Re-evaluate my life?

Natalia Ryan
3 min readDec 14, 2020

Week seven of my software engineering course at Flatiron School. We were instructed to create a Sinatra MVC application.

With the help of the ever wonderful Corneal Gem, it gave me the foundation I needed to start my project with confidence. It allowed me to create Doc-It, an application that allows the user to create and manage doctor’s appointments. This allows the user to have a has_many relationship with appointments. Making appointments have a belongs_to relationship with the user.

My immediate struggle with setting up my different controllers and connecting them to my view pages was trying to unravel the difference between rendering a file and redirecting a file. Before getting to that, we must understand the file path that is crucial to our app.

File path from Doc-It

Within the Doc- It application you will see several folders that all serve a different purpose. For this, we will focus on the MVC file structure.

App:

Holds our Controllers, Models, and Views folders.

>> Controllers

A Sinatra Controller is simply a Ruby class that inherits from Sinatra::Base. This is where a bulk of the code lives. These contain our RESTful routes that allow us to send our user to different pages throughout the application.

>> Models:

This hold’s the logic behind the application. It is where we establish our classes that inherit from ActiveRecord::Base. Within this we also establish our relationships with the user and in my case appointments.

User class contains validations(validates and has_secure_password) and has_many relationship with appointments
Appointment belongs to user.

>> Views:

This folder contains the code that is displayed in the browser to the user. Sinatra uses a templating engine called ERB, or Embedded RuBy to encapsulate the code that matches to the action being rendered.

This is where we get into the discussion of:

render vs. redirect

We are relying on our GET and POST actions to tell us whether to redirect or render a file. If we are rendering a view, we are relying on the file path and it typically isn’t a page the user can directly link to. This is more so for our program to be able to retrieve the information we are trying to display.

For instance, the code above shows the opening code to my application. As soon as they run the program, we will be rendering the welcome page. This is not something the user can look up by searching “/welcome”. However, if this were located within a different folder, we would need to explicitly tell our program the direct file path needed to access the page.

Redirect is a method that allows us to send users to a new view that can contain the content they just inputted to the database. Our post method allows us to tell the browser to create a new request to a different location. The above method is outlining what happens when a user logs in. Once the program is able to determine that the login credentials are valid, they redirect the user to the index.erb page. This is URL extension, and can be seen/searched for by the user. Typically, we do not need to include the entire file path when redirecting our user.

It took me hours of frustration and error messages to realize there was a difference between redirecting our user, and rendering files. I hope my above explanation can save you hours and from re-evaluating your life too.

You can view my project here. Any and all feedback welcome!

--

--