我在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
值。
答案 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
数组。
最后一步是出于可扩展性和空间复杂性的原因。
祝你好运!