How to access 'can?' method from within cell?
Asked Answered
S

2

7

I'm using cancan and cells gems in my ruby-on-rails project. How to access can? method from within cell? Thanks.

Shelburne answered 5/4, 2011 at 3:32 Comment(0)
I
12

I've had to do exactly this. Try

class MyCell < Cell::Rails

  include CanCan::ControllerAdditions

end                                        

If you're also using Devise, I had to do this:

class MyCell < Cell::Rails

  include CanCan::ControllerAdditions
  include Devise::Controllers::Helpers
  Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {}))

end                                        

#define_helpers will add helper methods such as current_user and user_signed_in? to the cell.

Invisible answered 19/10, 2011 at 10:30 Comment(2)
hi qnm! what is the last define_helpers part needed for?Miyokomizar
@Miyokomizar Take a look at the code - you'll see this brings the devise helpers - current_user / user_signed_in? etc. into the cell as helpers.Invisible
E
4

For those who happen to have a custom current_ability() method (in which you can change the name of current_user method and Ability class names):

class OrderCell < Cell::Rails
  include CanCan::ControllerAdditions
  delegate :current_ability, :to => :controller
end
Enchantress answered 25/4, 2013 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.