【问题标题】:rails undefined method for user modelrails未定义的用户模型方法
【发布时间】:2012-05-21 08:15:51
【问题描述】:

rspec 测试未通过此项目:

Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `picking?' for nil:NilClass

触发这个错误的代码是:

<% if current_user.picking?(post) %> 

在用户模型中,拣货方式定义为: def 采摘?(帖子) trips.find_by_pickedpost_id(post.id) 结束

current_user 定义为:

def current_user=(user)
  @current_user = user
end

def current_user
  @current_user ||= user_from_remember_token
end

为什么测试失败?为什么 current_user 是 nil 类?

【问题讨论】:

  • +0。句子以大写字母开头,并且您没有突出显示所有代码。

标签: ruby-on-rails undefined


【解决方案1】:

你确定user_from_remember_token 会返回一些东西吗?我建议使用debuggerpry 来调试您的代码。我发现后者更容易使用,但它似乎缺少第一个的功能。

【讨论】:

  • 是的,current_user 变量在系统的其他部分运行良好
  • @ksol,可以通过插件增强 pry 以提供调试器的大部分功能:banisterfiend.wordpress.com/2012/02/14/the-pry-ecosystem/
猜你喜欢
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2013-04-28
  • 2015-12-10
  • 1970-01-01
  • 2017-01-02
  • 2021-11-02
  • 2015-06-14
相关资源
最近更新 更多