Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 385
Daniel Avatar asked Jan 26 '26 05:01

Daniel


2 Answers

  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
like image 72
Jimmy Avatar answered Jan 27 '26 17:01

Jimmy


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.

like image 24
Naren Sisodiya Avatar answered Jan 27 '26 17:01

Naren Sisodiya