I'm using cancan
and cells
gems in my ruby-on-rails project.
How to access can?
method from within cell?
Thanks.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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