检查用户是否被关注

时间:2016-01-03 12:16:16

标签: ruby-on-rails angularjs

我在rails / angular app中创建了一个关注功能。在我的模板中,我有这个,

%ul{"ng-repeat" => "user in users"}
  %li
    name: {{ user.name }}
    %a{"ng-click" => "followUser(user)"} Follow user.

这需要此功能,

$scope.followUser = function (user) {
  $scope.user = user
  console.log ($scope.user)

  followUser.create({
    followed_id:   $scope.user.id
  })

  Notification.success($scope.user.name + ' is toegevoegd als vriend.');
}

要求此服务,

app.factory('followUser', ['$http', function($http) {
  return {
    create: function(user) {
      return $http.post('/relationships.json', user);
    }
  };
}])

然后在这里建立关系,

def create
  @relationship = current_user.active_relationships.build(:followed_id => params[:followed_id])
  if @relationship.save
    redirect_to root_url
  else

  end
end

这是用户模型,

has_many :active_relationships,  class_name:  "Relationship",
                                   foreign_key: "follower_id",
                                   dependent:   :destroy

has_many :passive_relationships, class_name:  "Relationship",
                                 foreign_key: "followed_id",
                                 dependent:   :destroy

has_many :following, through: :active_relationships,  source: :followed
has_many :followers, through: :passive_relationships, source: :follower

关系模型,

belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"

我想检查用户中的用户是否已被关注。如果是这样,模板应显示取消关注功能。所以我在想这样的事情,

%ul{"ng-repeat" => "user in users"}
  %li{"ng-if" => "user.followed == false "}
    name: {{ user.name }}
    %a{"ng-click" => "followUser(user)"} Follow user.
  %li{"ng-if" => "user.followed == true "}
    name: {{ user.name }}
    %a{"ng-click" => "followUser(user)"} Unfollow user.

因此,我希望在跟踪用户时将名为followed的值设置为true或false。所以我可以通过ng-if检查该值。但我不认为可以为每个不同的用户设置该值。

Bob可以关注Joe,将followed值设置为true。但这意味着现在每个用户都会关注Joe。那么如何仅为Bob设置followed值。

1 个答案:

答案 0 :(得分:2)

在当前用户的followed_id中添加所有active_relationships的数组,例如$scope.currentUser

例如,您的$scope.currentUser.activeRelationships应该返回类似[2, 3, 4]的内容,这些内容是用户跟随的ID。

创建一个方法来检查用户后面是currentUser。

$scope.followed = function (user) {
  $scope.user = user
  if ($scope.currentUser.activeRelationships.indexOf($scope.user.id) > -1 {
     return true;
  }
 else {
   return false;
 }
}

修改您的服务以添加destroy电话

app.factory('followUser', ['$http', function($http) {
  return {
    create: function(user) {
      return $http.post('/relationships.json', user);
    },
    destroy: function(user) {
      return $http.delete('/relationships.json', user);
    }
  };
}])

将您的unfollowUser方法添加到

$scope.unfollowUser = function (user) {
  $scope.user = user
  console.log ($scope.user)
  followUser.destroy({
    followed_id:   $scope.user.id
  })

  Notification.success($scope.user.name + ' werd verwijderd uit vrienden.');
}

在API控制器上创建destroy方法。

def destroy
  if current_user.active_relationships.where(:followed_id => params[:followed_id]).destroy_all
    redirect_to root_url
end

如果用户拥有大量关系,那么只需尝试根据您拥有的用户的视图划分数据。例如,如果您使用ID 1到10加载用户,并且当前用户关注的是ID为1和2的用户,则$scope.currentUser.activeRelationships应仅包含[1, 2]。您应该根据您的请求更新它以从服务器获取更多用户。喜欢以下

<强> users_controller.rb

def index
   users = User.page(params[:page])
   followed_by_current_user = current_user.active_relationships.where(followed_id: @users.pluck(:id)).pluck(:followed_id)
   render json: {users: @users, followed: followed_by_current_user}
end

然后使用此方法中的数据更新$scope.currentUser.activeRelationships数组。

最后一步是出于可扩展性和空间复杂性的原因。

祝你好运!