31

How can you output the current session to the view?

sscirrus
  • 55,407
  • 41
  • 135
  • 228
  • see https://stackoverflow.com/questions/55833240/in-rails-from-byebug-how-can-i-view-the-output-of-the-session-variable-as-a-str/55834018#55834018 . vasfed's answer of `session.to_h` shows some relevant aspects of session , the id and variables of it – barlop Apr 24 '19 at 18:31
  • Doesn't work as of Rails 5.1, `undefined method 'to_h' for #` – szeryf Mar 24 '20 at 10:25

2 Answers2

54

<%= session.inspect %>

Jed Schneider
  • 14,085
  • 4
  • 35
  • 46
  • 1
    inspect works almost anywhere to get more info than you to_s is giving you. I use it all the time – Jed Schneider Aug 12 '10 at 11:04
  • 3
    `session.to_h` is pretty good too as mentioned by vasfed here https://stackoverflow.com/questions/55833240/in-rails-from-byebug-how-can-i-view-the-output-of-the-session-variable-as-a-str/55834018#55834018 – barlop Apr 24 '19 at 18:32
7

To those reading, I found another answer that also seems to work: <%= debug session %>.

sscirrus
  • 55,407
  • 41
  • 135
  • 228
  • 1
    Both of the solutions posted work, although this one has the added benefit of enclosing the output within a `` element. Don't forget however, that it's extremely important to add a conditional like so: `<%= debug(session) if Rails.env.development? %>` – stephenmurdoch Oct 22 '12 at 23:13