Using Sessions in Basic Rails Apps

Mike Archer
5 min readOct 5, 2020

--

One huge key to learning how to be a successful developer is understanding the flow of an app or website. If you are a student at Flatiron like myself, you will learn about forms, collection.select, and the many different ways to pass your params from a form to create new objects. While this provides a great starting point, if you compare this process to your standard website or app, you will see it does not quite add up.

For this example, let’s use UberEats. When you select a restaurant from uber eats, it their system knows that if you create an order, that last restaurant you were viewing is where you would like to order from. UberEats also knows that you, the person logged in, is the on placing the order. If you try to implement this type of intuition yourself, you can easily use sessions to make it flow.

Without the use of sessions, this would process would be something like:

1. click “place order”.

2. redirect to a form that asks for whom the order is for, and what restaurant

3. pass the input back to your create method in the form of params

4. create an order using params for said restaurant.

create method using params from form

This would certainly work, but again, when have you ever had a modern website or app where you were logged in ask you who you were again? How can we make the app know intuitively that the last restaurant we views is in fact the where we want to order from?

in Sessions Controller

Enter Sessions. To tackle the user portion of this problem, we have a few options. If your app is requiring a log in, we are undoubtedly using sessions already to store the users ID to reference certain elements. When a user logs in, we can set a session identifier. to equal their user ID that persists until another user is logged in. Since we already have a session that references who is the current user, rather than request this information in a form, we can simply reference this to create our new Order object. In our create method, rather than passing in the params from our form to identify the user, simply call the session that holds the user ID. This is pretty straight forward, and again, if you are using any kind of log-in session to begin with this can be easily implemented.

Next, we can address the issue of intuitively knowing what restaurant we are ordering from. If we are following RESTful conventions, each restaurant will be viewed through it’s show page. In our controller, when a user navigates to a show page, we can implement another session to remember where we were viewing. For this example, when we land on a show page we will be creating a session called [:restaurant_id].

Just like we did for our user, instead of using params from a form to populate the order, we can call this session in our create method to tell our app that the order is being placed at the last restaurant we were viewing.

updated form as a confirmation button

With both of these steps implemented, we can eliminate fields from our form and turn our “submit” button in to a simple “confirm” button. Now, when we go to place an order, our app intuitively knows that the order will be for the currently logged in user (session[:id]), at the last viewed restaurant (session[:restaurant_id]). Just like that, we have created a more simplistic, intuitive flow to order our favorite food.

Another way we can implement sessions is to set an origin. In many websites and apps, you can view certain pages without being logged in, however to purchase an item or make a reservation, we are required to log-in and then sent back to the last page we were viewing. This can get tricky, usually when a user logs in our standard is to route them to an index or show page, and this is generally not a dynamic route. By using a simple if statement to determine if a user is in fact logged in or not, we can choose where they are re-directed to.

re-directing in sessions controller

These are just a few basic examples that I hope will point you in the right direction. By storing users paths and previous screens through sessions, we can eliminate the need to ask so many questions that a good app would already have the answers for.

As we progress into other languages and skills, there will inevitably be way to abstract this away even further, however I found that working through sessions in this manner helped me to dig deeper into the uses and abilities of sessions.

--

--

No responses yet