How to Use Sinatra Session

Sinatra clears session on post

I was suffering from the same issue as you: sessions were being cleared on post.

I have no idea why this works, but this is my solution:

#enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'your_secret'

I literally just replaced the enable :sessions bit with use Rack::Session::Cookie ... and now all is good in the world.

Simple Sinatra Sessions Not Working

You need to move the enable :sessions line inside your app class:

class MyApp < Sinatra::Base
enable :sessions
#...

With that line where it is now you have enabled sessions for the Sinatra::Application app, which is the app that is run when you use the “classic” style, but that isn’t run here. You need to enable sessions in your app.

With that change you should see the cookie headers being set, but you will need to use something like curl -vL -b cookie_file -c cookie_file http://localhost:4567/foo to see it actually work with curl (so that the cookies are stored and resent).

Ruby Sinatra Sessions in a JSX file?

If you want to read session related non-sensitive data on the client side, you can set a cookie from the server and read it using javascript on the client side.

You can set cookies with Sinatra as detailed here: http://www.sinatrarb.com/contrib/cookies

And you can read cookies with Javascript as detailed here: Read a javascript cookie by name

An alternative would be to render javascript on the server-side and store the value in a javascript variable. I believe you already tried this:

<%= return_url = sesssions[:return_url] %>`

But that is only setting a Ruby local variable, which is lost on the client side, maybe you want set that return_url as the href of the button you want to have clicked?

But, if you needed the variable inside a javascript click handler, just render a javascript variable in your ERB view:

var returnUrl = "<%= sessions[:return_url] %>";

With the above you should be access to the javascript variable returnUrl in a button click handler provided it's in the same or higher lexical scope.

How to set session[:expires] in rack session (Sinatra)

I tried to do this via an after filter in Sinatra but it didn't work, I guess it sets the session after after filters have run, so I knocked up a quick Rack filter and it appears to work.

require 'sinatra'

class SessionExpiryModifier
def initialize(app,options={})
@app,@options = app,options
end
def call(env)
warn env["rack.session.options"].inspect
t = Time.now.to_i.even? ? 10 : 60
env["rack.session.options"].merge! :expire_after => 60 * 60 * t
@app.call env
end
end

configure do
use Rack::Session::Cookie,
:expire_after => 60*60*3,
:secret => 'xxxx' * 10
use SessionExpiryModifier
end

get "/" do
session[:usr] = Time.now
env["rack.session.options"].inspect
end

However, that makes it a lot harder to get a conditional from the Sinatra app into the Rack filter to decide on which branch to take, but that depends on what your condition is. Perhaps inject something into the headers that the filter can read to make the decision.



Related Topics



Leave a reply



Submit