新手问题 [Fixed] Rails Tutorial - Chapter 11 - NoMethodError in Users#followers

springwq · 2014年06月08日 · 最后由 springwq 回复于 2014年06月09日 · 2088 次阅读

代码 : https://github.com/springwq/mytwitter

访问 followers的时候报错了,following 是正常的,不晓得哪里出错了?

报错:

Failures:

  1) UserPages following/followers followers
     Failure/Error: visit followers_user_path(other_user)
     ActionView::Template::Error:
       undefined method `email' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_User:0x007fd1799ea1e0>
     # ./app/helpers/users_helper.rb:3:in `gravatar_for'
     # ./app/views/users/show_follow.html.erb:5:in `_app_views_users_show_follow_html_erb___3786658985952186547_70268838744720'
     # ./app/controllers/users_controller.rb:61:in `followers'
     # ./spec/requests/user_pages_spec.rb:163:in `block (4 levels) in <top (required)>'

  2) UserPages following/followers followers
     Failure/Error: visit followers_user_path(other_user)
     ActionView::Template::Error:
       undefined method `email' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_User:0x007fd17a6116d0>
     # ./app/helpers/users_helper.rb:3:in `gravatar_for'
     # ./app/views/users/show_follow.html.erb:5:in `_app_views_users_show_follow_html_erb___3786658985952186547_70268838744720'
     # ./app/controllers/users_controller.rb:61:in `followers'
     # ./spec/requests/user_pages_spec.rb:163:in `block (4 levels) in <top (required)>'

  3) UserPages following/followers followers
     Failure/Error: visit followers_user_path(other_user)
     ActionView::Template::Error:
       undefined method `email' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_User:0x007fd17d7f0480>
     # ./app/helpers/users_helper.rb:3:in `gravatar_for'
     # ./app/views/users/show_follow.html.erb:5:in `_app_views_users_show_follow_html_erb___3786658985952186547_70268838744720'
     # ./app/controllers/users_controller.rb:61:in `followers'
     # ./spec/requests/user_pages_spec.rb:163:in `block (4 levels) in <top (required)>'

楼主把 User 的迁移文件贴出来看看,会不会是拼写错误

mytwitter / app / controllers / users_controller.rb

def followers
  @title = "Followers"
  @user = User.find(params[:id])
  @user = @user.followers.paginate(page: params[:page])
  render 'show_follow'
end

这里错了

你定义了两次 @user ,我估计第二个应该是 @users

#2 楼 @blacktulip 是的,是这个问题!谢谢!

有时,我知道自己肯定是哪里代码抄错了,但自己费好大劲就是找不出来,debug 也没什么思路,还需要勤加练习~~

需要 登录 后方可回复, 如果你还没有账号请 注册新账号