Rails:使用GetStream和Acts As Follower gems的通知

时间:2016-04-21 01:38:30

标签: ruby-on-rails getstream-io

我正在使用getstreamacts as follower为我的项目构建通知和新闻Feed。当关注某个用户时,该用户应该收到此类关注的通知。我可以关注和取消关注用户,但getstream会出现以下错误:

Something went wrong creating an activity: undefined local variable or method `user' for #<Follow:0x007ffaa70c9398>
Completed 500 Internal Server Error in 37ms

NameError (undefined local variable or method `user' for #<Follow:0x007ffaa70c9398>):
app/controllers/users_controller.rb:160:in `follow_user'


Rendered /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.1ms)
Rendered /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.8ms)
Rendered /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.9ms)
Rendered /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (108.1ms)

如果我的follow.rb文件看起来像这样:

class Follow < ActiveRecord::Base

  extend ActsAsFollower::FollowerLib
  extend ActsAsFollower::FollowScopes

  # NOTE: Follows belong to the "followable" interface, and also to followers
  belongs_to :followable, :polymorphic => true
  belongs_to :follower,   :polymorphic => true


  include StreamRails::Activity
  as_activity

  def activity_notify
    [StreamRails.feed_manager.get_notification_feed(self.followable_id)]
  end

  def activity_object
    self.followable
  end

  def block!
    self.update_attribute(:blocked, true)
  end

end

user.rb我有:

acts_as_followable
acts_as_follower

并没有引用Follow模型的其他内容。我有什么想法吗?

编辑:

以下是用户控制器中的follow和unfollow代码。第160行是if @follower.follow(@following),同样适用于取消关注:

def follow_user
  @follower = User.find_by_username(params[:username])
  @following = User.where(id: params[:following_id]).first
  @user = @following

  if @follower == @following
    respond_to do |format|
      flash.now[:warning] = "Cannot follow yourself."
      format.js {  render file: "/app/views/layouts/notice.js.erb" }
    end
  end

  if @follower.follow(@following)
    StreamRails.feed_manager.follow_user(@follower.id, @following.id)
    respond_to do |format|
      format.js { render file: "/app/views/users/follow.js.erb" }
    end
  else
    respond_to do |format|
      flash.now[:info] = "Error."
      format.js {  render file: "/app/views/layouts/notice.js.erb" }
    end
  end
end

def unfollow_user
  @follower = User.find_by_username(params[:username])
  @following = User.find(params[:following_id])
  @user = @following

  if @follower.stop_following(@following)  
    StreamRails.feed_manager.unfollow_user(@follower.id, @following.id)
    respond_to do |format|
      format.js { render file: "/app/views/users/follow.js.erb" }
    end
  else
    respond_to do |format|
      flash.now[:info] = "Error."
      format.js { render file: "/app/views/users/add_flag.js.erb" }
    end
  end
end

1 个答案:

答案 0 :(得分:0)

stream-rails gem假定您的模型具有user属性来查找活动的参与者。您可以通过在activity_actor模型中实施Follow方法来更改此行为。

例如:

class Follow < ActiveRecord::Base

    def activity_actor
        self.follower
    end

end

这将创建诸如&#34; x跟随y&#34;之类的活动。创建跟随模型时(以及您已经实现的逻辑,以在Feed之间创建实际跟随连接)。