使用AuthLogic在ROR中创建会话后,current_user为nil [英] Current_user nil after creating a session in ROR with AuthLogic

查看:96
本文介绍了使用AuthLogic在ROR中创建会话后,current_user为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AuthLogic和current_user方面遇到了一些问题.

I'm having a bit of problems with AuthLogic and current_user.

我有一个使用Cairngorm框架作为前端,使用Ruby On Rails作为后端的Flex4应用程序.

I have a Flex4 application using the Cairngorm framework as the front-end, and Ruby On Rails as the back-end.

仅使用ROR时,我可以通过浏览器登录.但是,当我通过Flex4应用程序尝试它时,它将第一次失败,但是第二次可以工作.

I can log in fine through a browser, and when only using ROR. However, when I try it through my Flex4 application, it will fail the first time but work the second time.

正在发生什么,在user_sessions_controller.rb内,我有一个呼叫

What is happening, is inside the user_sessions_controller.rb I have a call to

self.current_user.to_xml;

我第一次调用create动作时,current_user对象返回nil.我第二次调用它(无需重新启动服务器或浏览器),它将是当前的正确用户.

The first time I call the create action, the current_user object returns nil. The second time I call it (without restarting the server, or browser) it will be the proper current user.

因此,这使我相信在我的create动作中的render命令之后的某个时间设置了current_user.

So this leads me to believe that the current_user is being set sometime after the render command inside my create action.

如果我需要我的rails控制器在create动作中返回当前用户,我该怎么做?

If I need my rails controller to return the current user in the create action, how would I go about doing that?

推荐答案

只是存在完全相同的问题...不确定为什么会发生,但是我试图在user_sessions_controller.rb的create方法中调用current_user,已解决如下...

Was just having the exact same problem...not sure why this happens but I was trying to call current_user in my create method in my user_sessions_controller.rb, solved as per below...

  def create
    @user_session = UserSession.new(params[:user_session])

    if @user_session.save
      current_user = UserSession.find.user
      current_user.increment_login_count_for_current_memberships!
      flash[:notice] = 'Sign in successful.'
      redirect_to root_path
    else
      render action: "new"
    end
  end

希望这会有所帮助!

这篇关于使用AuthLogic在ROR中创建会话后,current_user为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆