设计:未定义方法“user_signed_in?”对于应用程序控制器:类
我在 Rails 3.1.1 和 Ruby 1.9.2 中遇到上述错误。我花了最后一个小时进行查找,我发现的主要内容是:
- ApplicationController 中的
clear_helpers
可能导致助手无法加载。我的代码没有使用它,而且我不相信我安装的 gem 也没有使用它。 routes.rb 中缺少
的devise_for
。我在routes.rb 中缺少devise_for :users
缺少“database_authenticatable”。我的 users.rb 中有:
设计:database_authenticatable,:可注册, :recoverable, :rememberable, :trackable, :validatable
我的行为与 此电子邮件线程,
signed_in 除外? :user
也不适合我。但是,Devise.mappings
确实显示了用户映射。
关于可能出什么问题的任何想法吗?即使对 current_user 进行 hack 也会对我有很大帮助,因为我必须尽快运行一些功能。
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:
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.Missing
devise_for
in routes.rb. I havedevise_for :users
Missing 'database_authenticatable' in my Users model. I have in my users.rb :
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatableMy 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
3号让我觉得很奇怪。您的模型文件名为 users.rb 吗?你的类声明是
Users < ActiveRecord::Base
?如果是这样,请将它们更改为以下内容。用户.rb => user.rb
用户 < ActiveRecord::Base
Number 3 strikes me as odd. Is your model file named users.rb? Is your class declaration
Users < ActiveRecord::Base
? If so, change them to the below.users.rb => user.rb
User < ActiveRecord::Base
所以我将其标记为社区 wiki,因为解决方案很简单。
我正在调用调试器(我用它来检查
current_user
,代码如下)Rails 使会话和请求对象可用于控制器的实例。该调试器在
self == ApplicationController 的上下文中调用
当它应该是self == #
长话短说时,请确保您没有尝试在控制器定义中调用它。 /em> 但在控制器内实例。
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 codeRails makes the session and request objects available to instances of controllers. That debugger is called in the context of
self == ApplicationController
when it should beself == #<MainController...>
.Long story short, make sure you're not trying to call it within a controller definition but within the controller instance.