2017-07-18 14 views
0

フォローしている/フォロワーに関して働くために使用される理由コードは、私がcancancanルールを必要としていると思われますが、わかりません。うまくいけば、誰かが私の間違いを見ることができます...rails 4 cancancan polymorphic

Profileモデル

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 

は、問題を解決しました。みんなの時間を無駄にして申し訳ありません。