简体   繁体   English

rails 3,heroku,如何使用会话

[英]rails 3, heroku, how to use sessions

I saw a great answer re how to setup sessions for rails 3 ( Rails sessions current practices ) 我看到了如何为rails 3设置会话的一个很好的答案( Rails会话当前实践

we gather a series of questions from the user and I'd like to maintain in session a list of their answers until we get to the end then write it all out... 我们从用户那里收集了一系列问题,我想在会议中保留他们的答案列表,直到我们结束然后全部写完......

but am not sure how to write and read info from the session... any quick pointer would be appreciated on how to save, for example, the contents of a hash 但我不确定如何从会话中写入和读取信息......任何快速指针都会被理解为如何保存,例如,哈希的内容

Also, does having our app hosted on a grid at Heroku change how we could/should handle sessions? 此外,我们的应用程序托管在Heroku的网格上是否会改变我们可以/应该如何处理会话?

Cheers, JP 干杯,JP

You do not have to change anything for Heroku. 你不必为Heroku改变任何东西。 By default, Rails sessions are stored in an encrypted cookie, so there is no server-side configuration necessary. 默认情况下,Rails会话存储在加密的cookie中,因此不需要服务器端配置。

However, a cookie can only store 4,096 bytes of data. 但是,cookie只能存储4,096字节的数据。 If you are storing a lot of data in the session (which generally is not recommended), you may overflow the cookie. 如果您在会话中存储大量数据(通常不建议这样做),您可能会溢出cookie。 In this case, you may wish to set up ActiveRecord or Memcached-based cookies. 在这种情况下,您可能希望设置基于ActiveRecord或Memcached的cookie。 Both of these are easy to do, and are not really Heroku-specific issues. 这两个都很容易做,并不是真正的Heroku特定问题。 If you need help with this, you can always ask another StackOverflow question. 如果您需要帮助,可以随时询问另一个StackOverflow问题。 For now, it's not worrying about until you hit the limit. 就目前而言,直到达到极限才会令人担忧。

Some rough code to store and read your answers in the session, assuming Question and Answer are ActiveRecord models: 在会话中存储和读取答案的一些粗略代码,假设问题和答案是ActiveRecord模型:

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

Sessions in Rails are very easy to use, just make use of the session hash-like structure as follows: Rails中的会话非常容易使用,只需使用类似会话哈希的结构,如下所示:

  • (set) session[:my_name] = "Joe" (设置)session [:my_name] =“Joe”
  • (read) puts session[:my_name] (读)放会话[:my_name]

I don't imagine you'll have to change anything for deployment on Heroku. 我不认为你必须改变在Heroku上部署的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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