我不确定这是否可行,但我想在我自己的 gem 中使用 CanCan。我试过以下代码:
module DynamicMenu
require 'engine' if defined?(Rails)
require 'cancan'
module MenuItems
#The new dynamic_menu encapsulates
class DynamicMenu
def initialize
@actionMenuItems ||= []
end
def newmenuitem(name,link,method=:get,confirm="Are you sure?")
genlink = ""
if method==:delete
genlink += "<a href=\"#{link}\" data-confirm=\"#{confirm}\" data-method=\"delete\" rel=\"nofollow\">
#{name}</a>".html_safe
elsif link==:submit
genlink += "<a href=\"#\" data-submit=\"true\">#{name}</a>".html_safe
elsif link == :back
genlink = "<a href=\"#\" data-back=\"true\">#{name}</a>".html_safe
else
genlink = "<a href=\"#{link}\">#{name}</a>".html_safe
end
@actionMenuItems << genlink.html_safe
end
def newCanCanProtectedItem(name,link,method=:get,confirm="Are you sure?")
actions = Rails.application.routes.recognize_path(link)
if defined?(CanCan) and can? actions[:action],actions[:controller].singularize.capitalize
newmenuitem(name,link,method,confirm)
else
@actionMenuItems << "CANCAN NOT DEFINED OR ABILITY.RB PROBLEM";
end
end
def getItems
return @actionMenuItems
end
end
end
end
这样做时,我得到未定义的方法can?
。