Pankaj
Pankaj

Reputation: 2558

How do you store and access session data in a rack based application

How do you store and 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?

Upvotes: 0

Views: 737

Answers (1)

xinit
xinit

Reputation: 1916

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.

Upvotes: 3

Related Questions