本文介绍了Devise:未定义的方法`user_signed_in? for ApplicationController:Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Rails 3.1.1和Ruby 1.9.2的上述错误。我花了最后一小时的时间,我发现的主要事情是:


  1. clear_helpers ApplicationController中的可能会导致帮助程序无法加载。我的代码没有使用,我不相信我安装的宝石也是。


  2. 缺少 devise_for 在routes.rb中。我有 devise_for:users


  3. 在我的用户模型中缺少database_authenticatable。我在我的users.rb中:



    devise:database_authenticatable,:可注册,
    :可恢复,可记忆,可追踪,可验证

    / li>
  4. 我的行为与,除了 signed_in? :用户也不适用于我。但是, Devise.mappings 确实显示用户映射。




解决方案

作为一个社区维基,因为解决方案是微不足道的。



我正在调用调试器(我用来检查 current_user 以下代码

  class ApplicationController< ActionController :: Base 
protect_from_forgery
调试器
过滤器{| c | Authorization.current_user = c.current_user}
end

Rails使会话并请求可用于控制器实例的对象。在 self == ApplicationController 的上下文中调用该调试器,当它应该是 self ==# MainController ...>



长篇小说,确保你不会在控制器中定义它,但在控制器实例中。


I'm getting the above error with Rails 3.1.1 and Ruby 1.9.2. I've spent the last hour looking and the main things I've found were:

  1. clear_helpers in ApplicationController can cause the helpers to not load. My code isn't using that and I don't believe the gems I've installed are either.

  2. Missing devise_for in routes.rb. I have devise_for :users

  3. Missing 'database_authenticatable' in my Users model. I have in my users.rb :

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  4. My behavior matches exactly what's described in this email thread except signed_in? :user doesn't work for me either. However, Devise.mappings does show the user mapping.

Any ideas on what could be wrong? Even a hack for current_user would help me greatly as I have to get some functionality running ASAP.

解决方案

So I'm marking this as a community wiki since the solution is trivial.

I was calling debugger (which I used to check current_user with the following code

class ApplicationController < ActionController::Base
  protect_from_forgery
  debugger
  filter {|c| Authorization.current_user = c.current_user}
end

Rails makes the session and request objects available to instances of controllers. That debugger is called in the context of self == ApplicationController when it should be self == #<MainController...>.

Long story short, make sure you're not trying to call it within a controller definition but within the controller instance.

这篇关于Devise:未定义的方法`user_signed_in? for ApplicationController:Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:39