未定义的方法"user_signed_in?"对于ApplicationController:Class,Devise [英] undefined method `user_signed_in?' for ApplicationController:Class, Devise

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

问题描述

以下代码:

class ApplicationController < ActionController::Base
  protect_from_forgery

  if user_signed_in?
  end
end

产生此错误:

undefined method `user_signed_in?' for ApplicationController:Class, Devise

我查看了此帖子,但我没有不知道他通过应用程序控制器实例与应用程序控制器定义的含义.我在config/routes.rb文件中设置了devise_for:users,未使用clear_helpers,而在user.rb文件中设置了:database_authenticatable.如果if语句处于不同控制器中的某个动作中,则它的功能就很好.为什么在这里不起作用?我需要以某种方式通过设计助手吗?

I looked at this post but I don't know what he means by instance of the application controller vs. definition of the application controller. I have devise_for :users set up in the config/routes.rb file, am not using clear_helpers, and have :database_authenticatable in my user.rb file. This if statement functions just fine if it's in an action in a different controller. Why is it not working here? Do I need to pass the devise helper in somehow?

推荐答案

他说的是使用控制器类定义本身而不是控制器类的实例来调用函数.在代码 self == ApplicationController 中,他正在将 self 与类定义 ApplicationController 的引用进行比较,而不是与特定实例(或对象)进行比较的 ApplicationController .

He's saying call the function using the controller class definition itself, not an instance of the controller class. In the code self == ApplicationController he is comparing self to the reference of the class definition ApplicationController rather than to a specific instance (or object) of ApplicationController.

在这种情况下,该帖子实质上是说要在控制器的方法中而不是仅在控制器定义中的某个地方调用 debugger 函数.

In this case, the post is essentially saying to call the debugger function within a method of the controller and not just somewhere in the definition of the controller.

如文章所述,您应该致电user_signed_in吗?在实例的上下文中,而不是在定义中.因此,将其放入要用于before过滤器之类的方法中.例如:

As the post mentions, you should be calling user_signed_in? within the context of an instance, not the definition. So put it in a method to be used for a before filter or something. For example:

class ApplicationController < ActionController::Base
  protect_from_forgery

  def find_user_name
     if user_signed_in?
        return user.user_name
     end
  end

end

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

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