简体   繁体   中英

Sessions in Rails 4

If ActiveRecord::Store is being deprecated, what is the suggested method for maintaining session? Is encrypted cookie store the new standard? What if you want to maintain >4kb in state?

In Rails 4, include gem 'activerecord-session_store' in your Gemfile and run bundle install. Complete the migration and configuration as per https://github.com/rails/activerecord-session_store notes.

The other alternative is using the 'dalli' gem for session management.

Look out for the file named session_store.rb inside your config/initializers folder. There you should setup which kind of session you would like to use. A standard, cookie based one, should be like:

ProjectName::Application.config.session_store :cookie_store, key: '_project_name_session'

After that all you need to do is call the hash session from within your controllers. You can set any value there and it will persist in between requests.

As in Rails Guides :

config.session_store is usually set up in config/initializers/session_store.rb and specifies what class to use to store the session. Possible values are :cookie_store which is the default, :mem_cache_store, and :disabled. The last one tells Rails not to deal with sessions. Custom session stores can also be specified (...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM