2017-07-18 46 views
0

無論出於什麼原因,用於關注跟隨/跟隨者的代碼不再有效,我懷疑我需要一個cancancan規則,但我不確定。希望有人可以看到我的錯......rails 4 cancancan polymorphic

剖面模型

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 

我已經嘗試過各種能力,但我覺得我只是在四處亂逛。如果上面沒有出現錯誤,是否有人能指出我應該放置的規則?

謝謝!

+1

嗨,這很難理解,它之前使用cancancan嗎?或者您剛剛添加了新功能?,Cancancan可以與用戶協作,您可以發佈配置文件並遵循控制器嗎?查看https://github.com/CanCanCommunity/cancancan/wiki/defining-abilities – Gaston

+0

@Gaston我按照你的要求添加了額外的代碼點,沒有Follows控制器只是一個模型來保持已更改的狀態。 –

+0

什麼不起作用?從你的問題不清楚。不允許你調用'follow'方法嗎? – coorasse

回答

0

我想出來了,它實際上並沒有因爲模型組織而被封鎖,這些動作在能力課上是不被允許的。

補充說:

can :follow, Profile 
can :unfollow, Profile 

解決的問題。抱歉浪費大家的時間。