1

I am using the session method of Ruby on Rails so that I have a session[:user_params] hash like this:

password_confirmation: "test"
password: test
email: test@test.ij

I can access that simply using the syntax session[:user_params] in my view file.

Now I want access only the 'email' parameter, but trying to use session[:user_params][:email], I get always an empty value. How to access this value?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
user502052
  • 14,803
  • 30
  • 109
  • 188
  • I don't think that sessions support other values than strings, but I'm not sure. But storing too much information in a session is never a good thing. Try serializing your hash if you must. – iain Dec 12 '10 at 16:08

2 Answers2

2

You might try session[:user_params]['email']. Off hand I'm not sure if Rails will serialize/deserialize the entire session hash as a HashWithIndifferentAccess or not.

aceofspades
  • 7,568
  • 1
  • 35
  • 48
1

I'm not sure how you are setting up the hash that you store in :user_params, but this is how I would do it and it seems to work:

puts "  email: '#{session[:user_params][:email]}'"

session[:user_params] ||= {}
session[:user_params][:password_confirmation] = "test"
session[:user_params][:password] = "test"
session[:user_params][:email] = "test@test.ij"

If you put this code inside a controller action the first time you will see '' for the email. The second time it will show 'test@test.ij' for the email. Hope that helps.

nimblegorilla
  • 1,183
  • 2
  • 9
  • 19
  • Thanks, but this does not help me. I want simply to show in my view file the email address using something like this: <%= session[:user_params][:email] %>. – user502052 Dec 12 '10 at 15:36
  • After you have put the email into session from a controller then you can access it from the view with the code that you just posted: <%= session[:user_params][:email] %> . – nimblegorilla Dec 12 '10 at 16:15
  • What I was aiming for was <%= session[:pjt_user_params]["email"] %> which I think is the shortest/simplest way to get what I want. – user502052 Dec 12 '10 at 17:35
  • If you are mixing :email and "email" as keys to the hash then that is probably why it doesn't work. Try evaluating :email == "email" from a rails console and you can see that they aren't equal. Try switching to use only :email. If that doesn't work post the rest of the code you are using and maybe there is something else. – nimblegorilla Dec 12 '10 at 17:53