如何了解rails中会话中的用户ID

时间:2016-06-14 09:54:15

标签: ruby-on-rails ruby ruby-on-rails-3 session login

我有一个rails应用程序。现在我想获取用户ID以将其存储到数据库中。但是系统显示错误,说id值是nill。

如何将用户ID存储在整数或字符串变量中?

 def create     
    user = User.authenticate(params[:email], params[:password])
    @data=Information.all
    @num=Information.count()
    @tym=Time.now.getutc
    if user
      session[:user_id] = user.id
      @val=session[:user_id]               # is it possible to store user id in variable @val
      #redirect_to root_url, :notice => "Logged in!"

      #this part is for showing the content of the information table


      respond_to do |format|
        format.html
        format.xml { render xml: @data }
      end
    else
     flash.now.alert = "Invalid email or password"
     render "new"
    end
 end

0 个答案:

没有答案