Rails中的新闻源,不明方法

时间:2014-03-22 16:41:42

标签: ruby-on-rails

我在rails中创建一个简单的新闻源。目的是让它返回用户所关注的组中的所有帖子。我正在使用socialization来执行以下功能。

确切的错误是:

NoMethodError (undefined method `followees' for false:FalseClass)

以下是我的基本型号,不包括likefollow,因为它们是空的:

用户:

class User < ActiveRecord::Base
  authenticates_with_sorcery!
  attr_accessible :username, :password, :email
  has_many :groups
  has_many :posts
  acts_as_follower
  acts_as_liker

  before_create :generate_auth_token

  def auth_token_expired?
    auth_token_expires_at < Time.now
  end

  def generate_auth_token(expires = nil)
    self.auth_token = SecureRandom.hex(20)
    self.auth_token_expires_at = expires || 1.day.from_now
  end

  def regenerate_auth_token!(expires = nil)
    Rails.logger.info "Regenerating user auth_token"
    Rails.logger.info "  Expiration: #{expires}" if expires
    generate_auth_token(expires)
    save!
  end
end

组:

class Group < ActiveRecord::Base
  attr_accessible :description, :name, :user_id
  has_many :posts
  belongs_to :user
  acts_as_followable 
end

发表:

class Post < ActiveRecord::Base
  attr_accessible :body, :user_id, :group_id
  belongs_to :user
  belongs_to :group
  acts_as_likeable
end

我在newsfeed控制器中设置了一个名为post的函数。该函数抓取用户正在关注的所有组,然后抓取返回的groups数组中group_id匹配group_id的所有帖子。但我一直得到unidentified method followees(社会化提供了这个)。然而,在irb中使用单个用户和帖子时似乎有效。

 def newsfeed 
    @groups = current_user.followees(Group)
    @posts = Post.where(:group_id => @groups)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @posts }
    end
 end

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

显然,您的current_user方法会返回false,而不是用户。检查从该方法返回的内容,找出导致错误的原因...

答案 1 :(得分:1)

您的current_user返回false而不是User实例。您可以从错误文本中看到它。

相关问题