Rails 3, heroku, how to use sessions

I saw a great answer on how to set up sessions for rails 3 ( Rails current practice sessions )

we collect a series of questions from the user, and I would like to keep a list of their answers in the session until we get to the end, and then write it all ...

but I'm not sure how to write and read information from the session ... any quick pointer will be appreciated how to save, for example, the contents of the hash

Also, does our grid-based application in Heroku have a change in how we could / should handle the sessions?

Cheers, JP

+8
ruby-on-rails session-variables session-state
source share
2 answers

You do not need to change anything for Geroku. By default, Rails sessions are stored in an encrypted cookie, so server side configuration is not required.

However, a cookie can only store 4,096 bytes of data. If you store a lot of data in a session (which is usually not recommended), you can overflow the cookie. In this case, you can set ActiveRecord or Memcached cookies. Both are easy to use, and are not really specific to Heroka. If you need help with this, you can always ask another StackOverflow question. So far this does not bother you until you reach the limit.

Incorrect code to store and read your answers in a session, assuming questions and answers are ActiveRecord models:

def store_answer(question, answer) session[:answers] ||= {} session[:answers][question.id] = answer.id end def read_answer(question) Answer.find(session[:answers][question.id]) end 
+18
source share

Sessions in Rails are very easy to use, just use a hash-like session structure like this:

  • (set) session [: my_name] = "Joe"
  • (read) puts session [: my_name]

I donโ€™t think you need to change anything to deploy to Heroku.

+4
source share

All Articles