无论出于什么原因,用于跟随/追随者的代码不再起作用,我怀疑我需要一个取消规则,但我不确定。希望有人能看到我的错误。
配置文件模型
class Profile < ActiveRecord::Base
...
has_many :follower_relationships, foreign_key: :following_id, class_name: 'Follow', dependent: :destroy
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key: :follower_id, class_name: 'Follow', dependent: :destroy
has_many :following, through: :following_relationships, source: :following
...
end遵循模型
class Follow < ActiveRecord::Base
...
belongs_to :follower, foreign_key: 'follower_id', class_name: 'Profile'
belongs_to :following, foreign_key: 'following_id', class_name: 'Profile'
...
end配置文件控制器
class ProfilesController < ApplicationController
...
load_and_authorize_resource
...
def follow
if current_user.profile.follow(@profile.id)
SystemMailer.following_email(@profile.user, current_user).deliver_later
redirect_to request.referrer
end
end
def unfollow
redirect_to request.referrer if current_user.profile.unfollow(@profile.id)
end 当前ability.rb
class Ability
include CanCan::Ability
...
can :edit, Profile, user_id: user.id
can :read, Profile
can :update, Profile, user_id: user.id
can :show, Profile
can :manage, Profile, id: user.profile.id我已经尝试了各种can能力,但我觉得我只是在胡乱操作。如果上面的代码行没有错误,有人能指出我应该实施的can规则吗?
谢谢!
发布于 2017-07-19 08:29:47
我弄清楚了,它实际上不是因为模型组织而被阻止,这些操作在能力类中是不被允许的。
添加:
can :follow, Profile
can :unfollow, Profile解决了问题。很抱歉浪费大家的时间。
https://stackoverflow.com/questions/45156081
复制相似问题