Model association validation allows the user to use CanCan

I have a TakeAction model that looks like this:

class TakeAction < ActiveRecord::Base
  belongs_to :user
  has_many :take_action_notes
  attr_protected :user_id  
end

and the TakeActionNote model, which looks like this:

class TakeActionNote < ActiveRecord::Base
  belongs_to :take_action
  validates_presence_of :note
end

Using CanCan, I am trying to allow the user who owns take_action to create and delete (delete) notes.

I tried to determine abilities with such a block.

 can :manage, TakeActionNote do |action, note|
    note.take_action.user.id == user.id
 end

I can add notes to take_action without any problems, but when I try to destroy it, I get:

NoMethodError in action notesController # 33 undefined `take_action 'method for nil: NilClass

Is there something I am missing or something is wrong? Any help is appreciated. Thank!

+3
source share
1 answer

TakeActionNotes. CanCan.

, Ability.rb, :

can :manage, TakeActionNote, :take_action => { :user_id => user.id }
+4

Source: https://habr.com/ru/post/1756077/


All Articles