我正在使用getstream并作为关注者为我的项目构建通知和新闻提要。当关注某个用户时,该用户应该收到此类关注的通知。我可以关注和取消关注用户,但 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
模型。任何想法我做错了什么?
编辑:
这是我在用户控制器中的关注和取消关注代码。第 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