ruby on rails - Can't use methods in the application helper for cancan ability.rb -


i'm using devise , cancancan. in application helper have -

def is_part_of_team?(book)     on_team = false     book.contributions.each |contribution|         if contribution.user == current_user             on_team = true         end     end     if book.user == current_user         on_team = true     end     return on_team end 

and in ability.rb have -

include applicationhelper ... can :update, book |book|   is_part_of_team?(book) end 

... throws error -

undefined local variable or method `current_user' #<ability... 

there better ways of doing this, wondered why cancan not current_user within application helper. why this? use function views , it's dandy.

my approach bad altogether.

instead of code in helper, hadn't understood use -

book.contributions.where(user_id: user.id).any? 

...to same result. means in ability.rb can have -

  can :update, book |book|     (book.try(:user) == user) || book.contributions.where(user_id: user.id).any?   end 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -