Daniel
Daniel

Reputation: 2381

Ruby on rails - flash variable

I'm completely new to rails. While learning I came across the use of the flash variable for maintaining data for the next postback when redirecting.

My questions are

  1. when should this be used.

  2. how does rails maintain it for me, does it make a round trip to the user and back or is it maintained server side.

  3. if it is maintained server side how does rails know when to discard the variable and prevent its memory getting clogged up.

Any replies would be appreciated

Upvotes: 3

Views: 3608

Answers (2)

Jimmy
Jimmy

Reputation: 9815

  1. Whenever you want to display any kind of status message or error message Your account settings have been updated! Your password or email is incorrect etc
  2. Rails loads it when you set it in your controller code and it is displayed in your view (if you have it set to display in your view <%= flash[:whatever] %>)
  3. Rails discards the value when it is rendered on the client

Upvotes: 6

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

flash is used to store the data (generally text) which required in next request and it get deleted automatically after the next request.

flash is nothing but an object which stores in session and maintain by rails itself (server side).

Rails initialize the flash object and marked it for removal in next request so it gets deleted in next request.

Upvotes: 3

Related Questions