Matt Eskridge
Matt Eskridge

Reputation: 1030

Ruby on Rails: Session Data Error

I'm pretty new to rails. I have a login system where, if data entered is valid, session[:user] is set. However, when later in ApplicationController I refer to session[:user] it will always give me an error, no matter the context, unless the line is commented out. Example:

user = session[:user]

The error it gives me is a generic "not working at this time" error, it doesn't actually say what's wrong.

Edit: Here's the error. It's a generic one.

We're sorry, but something went wrong.

We've been notified about this issue and we'll take a look at it shortly.

Also I tried many different variants and no matter what I stored in there it still gave me the error, it seemed. Here's my code:

Login processing:

session[:name] = valid_user.name
session[:password] = valid_user.password

Session validation (on every page view):

name = session[:name]
pass = session[:password]

Upvotes: 0

Views: 1364

Answers (2)

Andrew
Andrew

Reputation: 43113

Ok, a few things:

  1. You need to take your app out of production mode -- that's why you're getting this useless error message. Set to development mode or read your server log and you should get a more verbose and useful error message. ( See this question for related discussion, just set it to development instead of setting it to production as that asker was doing )

  2. Don't store the user object in session, and DEFINITELY don't store the password in session. You should only store the user id, then do something like this:

    user = User.find_by_id(session[:user_id]
    
  3. After you share the more verbose error message I can try to help you more, but most likely the error is you are asking for some session parameter that is undefined. The verbose error message will tell you what and where, but in the mean time check carefully to make sure you never ask for a session value when it hasn't been set.

Upvotes: 1

Spyros
Spyros

Reputation: 48616

Hmm, i don't see you initializing session[:name] anywhere. You are using session[:user] to do that. Is this really the case ?

I think that if you use :

name = session[:user]

it will work.

Upvotes: 0

Related Questions