user836087
user836087

Reputation: 2491

What exactly is a session in rails? from a coding point of view, how does it relate to controllers, models and views?

Can you please provide a simple example, as I am a noob in rails.

Thanks.

Upvotes: 2

Views: 357

Answers (2)

Matilda
Matilda

Reputation: 1718

Sessions are a way for you to save uses action or data on the frontend across multiple requests and then pass them to the backend. You'd get the data on the backend in the controller and then use it there. this is a good explanation http://guides.rubyonrails.org/security.html#sessions

For example lets say you have a forum where it's open for users to comment on the page, but they can only post their comment if they are logged in. So right when they click on post if they are not logged in or registered they would have to log in. Generally in Rails then you would let them log in or register and it would redirect them to another page, or even the current page. The problem is that they loose all the data they typed, so you want to be able to save that data and put it back on hte page or as they requested save it in the database. For example you have the following in your html

<%= reply_form.text_area :body %>
  ...
<% end %>

then in your controller you'd have

if session[:text_area]
  params["comment"] = session[:text_area]
  ....

that way you pass in session as expected to the params and the controller would get the comment and create a new comment object for you.

Another good example is shopping carts. If you don't have session enabled they you can't shop in online sites. The reason is you put something in your shopping cart and then you either browse more or go to the page to buy the item. Every time you click on a new page it sent a new request to the server and sent you new data, if you don't save the items to be purchased in a session then, there is no otherway to see what was saved in the last request so you loose your shopping cart data.

Upvotes: 2

Wayne Conrad
Wayne Conrad

Reputation: 107989

A session is a dialog between the server and the client; that dialog consists of one or more requests and responses.

Rails stores a bundle of variables for each session, so that you can have per-user state that persists between requests. To access the session in your controller, use the method session. It behaves like a hash, except that it is automatically persisted between requests. If you store something in the session during one request:

session[:foo] = 'foo'

Then you can get it back in a later request:

@foo = session[:foo]

The session store is most often (but not exclusively) used for authentication. If you are using an authentication gem such as devise, it uses the session store to keep track of who's logged in.

The session store in rails (and in other web servers) is implemented as a key/value store which is indexed by a key, typically generated at random for a given session. The server stores that key in a cookie which is gives to the client; the client's browser gives that cookie back with each request. The server retrieves that key from the cookie and uses it to retrieve the session state. Rails takes care of all of this for you so you don't typically have to worry about it.

The session store is managed by controllers. Views and models cannot access it without trickery.

Upvotes: 2

Related Questions