首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4 cancancan多态

rails 4 cancancan多态
EN

Stack Overflow用户
提问于 2017-07-18 09:37:07
回答 1查看 128关注 0票数 0

无论出于什么原因,用于跟随/追随者的代码不再起作用,我怀疑我需要一个取消规则,但我不确定。希望有人能看到我的错误。

配置文件模型

代码语言:javascript
复制
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

遵循模型

代码语言:javascript
复制
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

配置文件控制器

代码语言:javascript
复制
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

代码语言:javascript
复制
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规则吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-07-19 08:29:47

我弄清楚了,它实际上不是因为模型组织而被阻止,这些操作在能力类中是不被允许的。

添加:

代码语言:javascript
复制
can :follow, Profile
can :unfollow, Profile

解决了问题。很抱歉浪费大家的时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45156081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档