Params2session
Why
- To simplify common patterns, related to saving session variables.
- To store variables in "per-controller scope". For example, in
UsersController
, callingsession_save(:mode)
will saveparams[:mode]
tosession['users.mode']
Installation
Add this line to your application's Gemfile:
gem 'params2session'
Then execute:
bundle
Usage
This gem can be used in two or more steps:
- Save variable from params to session
- (optional) Set default value
- Get variable from session
Examples:
In controller:
session_save :mode
do_some_stuff if session_param(:mode)
Or:
session_save :mode
session_default :mode, 'all'
do_some_stuff if session_equal?(:mode, 'none')
Or:
session_save :mode
session_update(:mode, 'all') if session_param(':mode') == 'none'
do_some_stuff if session_equal(:mode, 'some')
View layer
session_param
and session_equal?
helpers are also available in views.
session_equial?(param_name, value)
can be used in views to set class
on buttons or button groups depending on saved param state, or to render
different partial based on saved param.
License
This project rocks and uses MIT-LICENSE.