Alex
Alex

Reputation: 11137

ruby on rails session information grab

When i run in my page this code :

<% user = User.find(session[:userid]) %>

i get the error :

line #1 raised:

Couldn't find User without an ID

although i have in my authentification in my sessions_controller this :

def create
    if user = User.authenticate(params[:username],params[:password])
      session[:user_id]= user.id
      session[:language_id]= user.language_id
      User.find(user.id).update_attributes(:last_login => Time.now)
      redirect_to root_path , :notice => (I18n.t :"session.login_success")
    else
      flash.now[:alert] = (I18n.t :"session.error")
      render :action => 'new'
    end
  end

and the session should contain the userid

Upvotes: 0

Views: 464

Answers (2)

JB Juliano
JB Juliano

Reputation: 632

Your controller is looking for params session[:user_id], but in your views it is session[:userid] so it will complain that session[:user_id] is nil.

Upvotes: 1

Romain
Romain

Reputation: 12809

In your log-in you set session[:user_id], and you try to find session[:userid] (note the spurious underscore). That's why.

Upvotes: 4

Related Questions