How do you store and access session data in a rack based application
How do you store an开发者_C百科d access session data in a rack based application. I am building a rack based application in ruby, i need to store data in session how do I do it?
That's quite an open question. Do you want to store data server side or client side? In the first case, you can store data in cookies:
def call
status, headers, body = @app.call(env)
response = Rack::Response.new body, status, headers
response.set_cookie("foo", {:value => "bar", :path => "/", :expires => Time.now+24*60*60})
response.finish
end
In the latter case, you you'll probably want to store it in a database (you can use ActiveRecord or other object mappers), or just a plain text file.
TL;DR: Look at frameworks for creating and storing sessions.
精彩评论