简体   繁体   English

Current_user设计方法在用户控制器外返回nil

[英]Current_user devise method returns nil outside the user controller

Is this a scope/routes problem? 这是范围/路线问题吗? The current_user works in the user controller. current_user在用户控制器中工作。 Outside, in another controller/view, this returns nil. 在外部,在另一个控制器/视图中,这将返回nil。 Even if the user is logged in, the user_signed_in? 即使用户已登录,user_signed_in? method returns false. 方法返回false。 I know I am missing something, just not able to point my finger. 我知道我错过了什么,只是无法指出我的手指。 What is the right way to do this? 这样做的正确方法是什么?

It works for me in other controllers. 它适用于其他控制器。 A wild guess: maybe the other controller needs: before_filter :authenticate_user! 猜测:也许其他控制器需要:before_filter:authenticate_user!

Found the problem. 发现了问题。 It was the sessions. 这是会议。 Apparently, the current_user and other methods were not behaving right because of cookies. 显然,current_user和其他方法由于cookie而表现不正常。 I had subdomains and the problem was because of that https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-%28Walkthrough%29 . 我有子域名,问题是因为https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-%28Walkthrough%29 This git tutorial helped. 这个git教程有所帮助。

Thanks! 谢谢!

我遇到了同样的问题,我需要把它放在我的布局中,我使用的是与应用程序不同的布局:

= csrf_meta_tags

Same thing happened to me but the problem was when I made an ajax request. 同样的事情发生在我身上,但问题是当我提出ajax请求时。 This fixed it (in haml). 这修好了它(以haml为单位)。 This makes sure that all ajax request carry the csrf token: 这可以确保所有ajax请求都带有csrf令牌:

:coffee
  require ['jquery'], ($)->
    $.ajaxSetup
      headers:
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')

Make sure this is in the header: 确保它在标题中:

= csrf_meta_tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM