Asa Bour
Asa Bour

Reputation: 23

When using ruby-on-rails how do you iterate over variables stored in the session?

I want to loop through all the variables stored in the session. I checked and it appears that sessions are stored as a hash: request.session.kind_of?(Hash) - returns true

I wasn't sure why the following code didn't work: request.session.each {|key, value| puts keys + " --> " + value

I am trying to output all session variables as part of a debug view.

Upvotes: 2

Views: 2129

Answers (3)

Weston Ganger
Weston Ganger

Reputation: 6712

request.session.to_hash.each{|key, value| puts "#{key.to_s} --> #{value.to_s}"}

Upvotes: 1

srboisvert
srboisvert

Reputation: 12749

<%= debug session %> might be easier.

Use it like this:

<% if ENV['RAILS_ENV'] == 'development' %>
    <%= javascript_include_tag 'prototype' %>
           <%= debug session %>
           <%= debug params %>                   

<% end %>

Upvotes: 1

Tomas Markauskas
Tomas Markauskas

Reputation: 11596

Instead of:

request.session.each {|key, value| puts keys + " --> " + value

Use:

request.session.each {|key, value| puts key.to_s + " --> " + value.to_s }

Upvotes: 4

Related Questions