Session problem

Hello everyone,

I am just starting a project for school in RoR and I am a complete n00b ^^ Here's my problem :

I need to get the user's login stored in the session but for some reason I cannot. Here's my login method code :

def login     if request.post?       @user = User.find_by_username(params[:login])   if @user and @user.password_is? params[:password]      session[:user] = @user.id

           redirect_to :controller => 'games'   else      @auth_error = 'Wrong username or password'   end     end   end

And here's the code of my GamesController. User needs to be logged in to access the games webpage (using check_auth method) and on that page the user can create a new game (adding a record in the game table, with a few fields from the form filled by the user and a field with the user's username, that's why I need to get it from session[:user]).

before_filter :check_auth

def check_auth     unless session[:user]       flash[:error] = 'You need to be logged in to access this panel'       redirect_to :controller => 'users', :action => 'login'     end   end

def create     @game = Game.new(params[:game])     @user = User.find(session[:user])     @game.host = @user.username

    respond_to do |format|       if @game.save         format.html { redirect_to(@game) }         format.xml { render :xml => @game, :status => :created, :location => @game }       else         format.html { render :action => "new" }         format.xml { render :xml => @game.errors, :status => :unprocessable_entity }       end     end   end

Thank you very much in advance for any help you could give, I really need to get this working to finish my project ^^

Blaede from Belgium

To understand how to require an user to log in to complete certain tasks, I would recomment you to read this excellent tutorial:

http://www.railsforum.com/viewtopic.php?id=14216

Regards,

Rey9999

Thank you very much for your answer Rey. I took a look at the tutorial and it seems very interesting indeed.

However, my application is very simple, all pages are accessible by all, except one (the games page) accessible by logged users only.

This feature works as it is on my website, my problem comes from the fact I have troubles extracting information from the session[:user]. I need to access the fields of the logged @user.

My project must be finished by the 5th of June so if someone could help me solve this problem without re-designing my entire application, I'd be really gratefull.

Thanks again

You haven't actually said what exactly goes wrong.

Fred

You haven't actually said what exactly goes wrong.

Fred

[QUOTE]I need to get the user's login stored in the session but for some reason I cannot. [/QUOTE]

On one hand I have : session[:user] = @user.id and on the other hand (in another controller) I have : @user = User.find(session[:user]) which if I am not mistaken should put in @user the session's user. But when in the next line I try to do : game.host = @user.username (game has a field host containing a string) and then try to do a game.save, rails tells me the host field cannot be null...

Blaede wrote:

You haven't actually said what exactly goes wrong.

Fred

[QUOTE]I need to get the user's login stored in the session but for some reason I cannot. [/QUOTE]

that told us only, that "something" went wrong :wink:

On one hand I have : session[:user] = @user.id and on the other hand (in another controller) I have : @user = User.find(session[:user]) which if I am not mistaken should put in @user the session's user. But when in the next line I try to do : game.host = @user.username (game has a field host containing a string) and then try to do a game.save, rails tells me the host field cannot be null...

better that. if session[:user] would be empty, then the find would have told you, that it can't find a user without an id. so that id is stored in the session.

so @user.username is nil or empty or doesn't exist. maybe misspelled?

Hi Blaede,

Blaede wrote:

On one hand I have : session[:user] = @user.id and on the other hand (in another controller) I have : @user = User.find(session[:user]) which if I am not mistaken should put in @user the session's user.

It will put the object retrieved in your User.find into @user. Problem is the User.find is returning nil. So you need to figure out why that is. A three things come to mind right off the bat. 1) It could be that session[:user] is nil because @user.id is nil when you set session[:user]. Do a puts right before you set session[:user] to check the value of @user.id in the console, and do likewise afterwards to see the value of session[:user] 2) It could be that session[:user] is not an int, which is what User.find() expects. You can check that with session[:user].is_kind_of?(int) (check the syntax) or you can just force it with User.find(session[:user].to_int) 3) It could be that you're invoking the controller/method that uses session[:user] before the one that sets it.

Use puts to display the values as your app executes and you'll figure out which it is, or what else it is if it's not one of these.

HTH, Bill