There is a mechanism available in ActionFlow which enables users to store data inside what is called a flow state. It is imlemented as a Hash object which offers a historical snapshot of himself according to a user’S position inside a flow. This allows users to hit the back button and restore exactly the flow state they were in at that precise point in the flow.
state attribute. The state attribute can be accessed the same way as the session. It’s nothing more than a Hash object after all.
class MyController < ActionFlow::Base def initialize # Some mapping here end def step_1 # Store data relevant to the current state state[:my_variable] = :some_value render state[:my_variable] end def step_2 # print the state variable value render state[:my_variable] end def step_3 # Modify the state data state[:my_variable] = :some_other_value render state[:my_variable] end end
In the previous example, going through this flow would print, in order, the following.
some_value
some_value
some_other_value
some_value
The mechanism underneath this is really quite simple. The view which submits events to the controller has to send back a flow execution key which tells the ActionFlow Controller two things :
Every time that the flow receives a new request originating from an already existing flow, meaning that a flow execution key was submitted, the current content of the state attribute is cloned to a new placeholder which corresponds to the state id part within the key. Therefore, we are able to restore the contents of the state hash if the user gets back in the flow.
See the Session Handler page for more details about the state persistence mechanism.
~~DISCUSSION~~
This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 License.