Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top