In terms of passing data amongst HTTP requests there are really two options:
-
Param or form data passing
-
Storing in cookies
-
Storing in server-side session, keyed off an ID stored as a cookie.
For a certain application where you’re using number 3, this comes at a cost - the session is “global”. In other words, in a single browser window with multiple tabs, the server side cannot distinguish between individual “sessions” within that browser session. This gem introduces a fourth, more granular method:
-
Storing in server-side session, keyed off an ID stored as a cookie and keyed off a request parameter.
This could be useful in instances such as a very complex wizard where transmitting the entire history of data RESTfully via param passing may not be feasible, yet you’d still like to support multiple tabs within the same browser window separately.
This gem requires ActionPack and ActiveSupport 3.0 or greater.
Include the gem in your Gemfile:
gem "micro_sessions"
Simply include the middleware in your controller:
class ApplicationController micro_sessions end
From there you will have access to a micro_session
object in your controllers and views, just like the session
object.
def new micro_session[:key] = "Value" end
By default the name of the request parameter used to differentiate micro-sessions is “_msid”, though this can be configured:
class ApplicationController micro_sessions :key => "_msid" end
By default the key generated will be a 10-digit random hash, though this can be configured as well:
class ApplicationController micro_sessions :type => :integer, :length => 10 end
Valid values for :type
: are:
-
:random_hash
-
:integer
(simply increments from one) -
:random_integer
(random integer with a maximum of:length
digits)
Session data is accessible to the controller and view through the micro_session
object, which works identically to the session
object but it specific to the micro-session:
micro_session[:key] = "Value" micro_session[:key] # => "Value"
When transitioning between pages for which you’d like to keep the micro-session alive, the request parameter must be passed along with any link, form, or redirect. The preferred way is to make use of the new :micro_session
option on available on the url_for
helper, which will accept either a specific value or just “true” to use the “current” micro-session to this request:
link_to user_path(@user, :micro_session => true)
Note that depending on the context in which you’re producing this URL, it may not work correctly. Unfortunately the low-level url_for call in the Rails routing engine does not have access to the controller object (and hence the micro_session object), so I can’t make this work outside of simple ActionView helpers. You can always explicitly include the URL options by calling:
micro_session.url_options # => {"_msid" => "0123456789"}
Can be used something like this:
redirect_to user_path(@user, micro_session.url_options)
If you wish to transmit the identifier via a form, be sure to include the hidden field:
<% form_for(@user) do |f| %> <%= micro_session_hidden_field %> ... <% end %>