Have you ever considered the life of a params flash session?

How long does a session or flash hold data?

When creating a website with rails, the defined variables etc. disappear immediately when the screen is changed. I think that instance variables such as @user have often died in one screen transition. I heard that __session and flash will live longer, so I want to see their lifespan! I wrote the article out of curiosity.

Experiment start

This time I made it forcibly on my PF, so it's a little hard to see.

① Define the participants of session flash params in the controller

It may be a difficult image, but it can be defined casually.

controller


  def top
    session[:life]="I am session. It is alive"
    flash[:life]="I am flash. It is alive."
    params[:life]="I'm params. It is alive."
  end

② Added a description to display players on the view page

View


            <%= session[:life]%><br>
            <%= flash[:life]%><br>
            <%= params[:life]%><br>
            
            <%= yield %>

③ Make several screen transitions to see how long you can live

0th transition

Confirm that everyone is displayed. スクリーンショット 2021-01-18 13.11.11.png

1st transition

As early as params take a breath. スクリーンショット 2021-01-18 13.11.25.png

2nd transition

Flash is also exhausted here. スクリーンショット 2021-01-18 13.11.34.png

Conclusion

name lifespan
params 1 transition kill
flash 2 transition kill
session Live until you kill

If the user's login status is saved in session, You can also be convinced that you are logged in all the time. Logout was destroying the login status of the user saved in session.

Recommended Posts

Have you ever considered the life of a params flash session?
Have you ever thought about the definition of "bad design"?
[PostgreSQL] The story that you have to cut the session properly
A memorandum of the FizzBuzz problem