I’m currently learning Sinatra, and I can’t seem to set a session variable to nil… I’ve been searching for hours, and it just doesn’t work. The strange thing is that it works locally on my machine, but it doesn’t work on Heroku. In short, my code looks like this:
configure :production do
enable :sessions
set :session_secret, ENV['SESSION_KEY'] || 'whatever'
end
post '/send-operation/?' do
session[:message] = 'Operation completed!'
redirect '/operation/'
end
get '/operation/?' do
if(session[:message])
"The message is: #{session[:message]}."
session[:message] = nil
end
end
So if I call the “send-opration” route, it redirects me to the “operation” route and it displays the session[:message] variable. If I refresh the “operation” page, there shouldn’t be any message since the previous message has been set to nil. But it still displays “Operation completed!” everytime I call the “operation” route. Am I doing something wrong?
thanks for reading!
You should rather use rack-flash instead of the session directly for setting status messages. The flash item will automatically be deleted after each request.
In your controller:
I use HAML for my templates, so this is what it looks like:
Hope that helps!
EDIT:
Also take a look at sinatra-redirect-with-flash