setting sinatra session var from javascript

262 views
Skip to first unread message

MilesTogoe

unread,
Oct 28, 2009, 9:31:23 PM10/28/09
to ur...@googlegroups.com
I use jquery to get an object's id from a select change.  So I have the id in an ordinary javascript var.  In the javascript function is there a way I can set the id into a session var ?   sort of like <% session[:obj_id] = jsvar %> .   The only way I know how is to pass the js var as a param and in the controller set the session var to the param (but then I have to write a bunch of validate checking on the param).  thks for any help

BJ Neilsen

unread,
Oct 28, 2009, 9:38:46 PM10/28/09
to ur...@googlegroups.com
Hope I didn't misread the question, but here goes. If you mean a sinatra session var, you could pass it through with an ajax request, something like:

$.post("http://yoursite.com/auth/add_param_to_session", {id_to_assign: theId}, function(){ alert("post went fine") });

Obviously the blatant controller action name should be different, and I would implement some kind of authentication of param afterwards (not just validating) to ensure that the var should be set. At least, that's my take.

BJ

Mike Moore

unread,
Oct 28, 2009, 10:24:56 PM10/28/09
to ur...@googlegroups.com
You probably don't want to give browsers the capability to modify your session data directly. But you could just as easily create or modify a non-session cookie. Then you just need to make your Sinatra code look for a cookie in addition to the session contents. 

Sent from my iPhone

MilesTogoe

unread,
Oct 29, 2009, 12:37:15 AM10/29/09
to ur...@googlegroups.com
okay, thks all for the suggestions.  yeah I just passed it back to the controller and wrote some code to check for security - I thought there might be a chance I could skip the page refresh but guess not, at least in a good fashion. 

Ryan Shaw

unread,
Oct 29, 2009, 12:00:06 AM10/29/09
to ur...@googlegroups.com
you dont need to set a "session" variable for this, cookies work just fine. this really simple sinatra app should show you everything you would need to do. 
Note there are jquery plugins to handle getting and setting cookie variables much better then just hard coding like I do here but the idea is the same:

require 'rubygems'
require 'sinatra'
get '/' do
 
  #here is how you can set a cookie from sinatra
  response.set_cookie("other_thing_id", 7)
 
  # note: the next 2 things will go to the output on the command line but not to the html rendered in the browser
 
  # check out whats in the cookie
  puts "here is all the cookies: " +request.cookies
 
  # get at that "thing_id" cookie that we set in the view after the first time they come to this page
  puts "here is the thing_id in the cookies: " + request.cookies["thing_id"].to_i
  # now go do something with it
 
  # here is what the view would look like
  'I am going to set a cookie <script> document.cookie = "thing_id=4" </script> here it is <script>alert(document.cookie)</script>'
 
end

Ryan Shaw

unread,
Oct 29, 2009, 12:05:28 AM10/29/09
to ur...@googlegroups.com
sorry, I should probably run my code before I send it.  here it is working:



require 'rubygems'
require 'sinatra'
get '/' do
 
  #here is how you can set a cookie from sinatra
  response.set_cookie("other_thing_id", 7)
 
  # note: the next 2 things will go to the output on the command line but not to the html rendered in the browser
 
  # check out whats in the cookie
  puts "here is all the cookies: #{request.cookies}"

 
  # get at that "thing_id" cookie that we set in the view after the first time they come to this page
  puts "here is the thing_id in the cookies: #{request.cookies["thing_id"].to_i}"

  # now go do something with it
 
  # here is what the view would look like
  'I am going to set a cookie <script> document.cookie = "thing_id=4" </script> here it is <script>alert(document.cookie)</script>'
 
end



MilesTogoe

unread,
Oct 29, 2009, 12:16:53 PM10/29/09
to ur...@googlegroups.com
okay, thks for info.
Reply all
Reply to author
Forward
0 new messages