Saving state on requests from the same user

I understand that state can be stored through cookies and the session[] hash.

Which of the following can be used to store ACROSS requests from the same user? Select all that apply.

  • Cookies
  • Hash session[]
  • Instance Variables Set Using the Controller Method
  • Class Variables Set Using the Controller Method

The answer to this quiz question was (1) and (2). My question is: why is it impossible to (3) and / or (4) save state on requests from the same user?

+4
source share
1 answer

(3) cannot save state through requests. Each time a request enters your Rails application, an instance of your controller is created and the corresponding action method is called on it. After the request has been processed, the controller instance is discarded, and a new new one is created for the next request.

(4) can technically save state on request, but data will be available for all instances of your controller, regardless of which user is executing the request. It also cannot be accessed from other controllers, it is lost if the server reboots, and if several instances of your Rails application are launched immediately (which is quite common in production scripts), the contents of the class variable will not be transferred between them.

Developing why such a thing is a bad idea: fooobar.com/questions/266663 / ...

+6
source

Source: https://habr.com/ru/post/1494850/


All Articles