2

In Rails its easy to store and retrieve session variables.

for e.g

session[:user_id] = @current_user.id

I was wondering if there is something this like in rhodes too.

Like in rails request parameters are accessible through

params['name']

while in rhodes its

@params['name']

If there is nothing like session variable, can anyone suggest some work around for managing sessions. like using global variables that are available across multiple requests.

Comments/Hints, please?

thanx.

hitesh israni
  • 1,742
  • 4
  • 25
  • 48

2 Answers2

2

I implemented my own session in rhodes. Assuming that a rhodes app will have only one user you don't need to detect in wich user or "session" you are, so you will have only one session. I created a global hash, named $session, where you put and get your values:

In the application.rb I put this:

$session ||= {}

And I use it thus:

$session[:user] = 'john'

puts $session[:user]
Douglas Lise
  • 1,466
  • 1
  • 20
  • 46
  • 1
    yes, that can be a work around. For now I am using some static variable inside a class(say UserInfo) in application.rb. I am setting that variable after login and putting nil on logout. I thought using global variable/s wont be a good design. can u comment on this. and thnx for your answer :) – hitesh israni Apr 17 '12 at 17:15
  • OK, using global variables is disencouraged, but I did this in the start of development and when I was learning ruby, so I did not know the right patterns. In my opinion your approach is correct and better than the mine – Douglas Lise Apr 17 '12 at 20:15
  • Your answer is one of the possible solutions, so i would vote it. though i would wait for some one to answer or comment a better solution. – hitesh israni Apr 17 '12 at 20:27
  • At this stage, I too am learning ruby, rails(so i am waiting for still better solution). Since you must have worked a little more on rho-mobile, can u recommend some references to get on with rhomobile/rhodes? I am kind of newbie to this mobile stuff. i have some knowledge of rails. I can see many references/screen casts for rails but not much for rho mobile/rhodes. thnx in advance. – hitesh israni Apr 17 '12 at 20:32
  • OK, Thank you by the upvote. The unique references I have found were the rhomobile docs page and the forum. Althoug, I have learned a lot by studying the rhomobile source code, inside the rhodes' gem or directly in GitHub (github.com/rhomobile/rhodes). – Douglas Lise Apr 18 '12 at 11:45
  • 1
    I know I'm kinda late but I found this: http://docs.rhomobile.com/rhodes/application#appapplication-class – doritostains Sep 20 '12 at 06:02
0

You could also store persistent values in the Redis database as a key/value pairs database.

Martlark
  • 14,208
  • 13
  • 83
  • 99
  • thnx. can you also provide some references/example for the same? it would be great.. – hitesh israni May 08 '12 at 14:31
  • can we use redis database on client side(on mobile phone) also? because i guess it runs on server side – hitesh israni May 10 '12 at 09:16
  • You're getting a bit beyond my meagre RhoMobile skills here. If you do not allow that class/object to sync with the remote server the data/value pairs will remain unique to that phone. So you could use it as a persistent storehouse for that phone/user. – Martlark May 11 '12 at 01:42