如何执行以下代码?来自数据库中的CanCan能力

时间:2015-10-21 08:39:46

标签: ruby-on-rails-4 cancan cancancan

我是从here得到的,但我不知道放在哪里以及如何执行此操作。

我尝试创建类并通过控制台(rails c)运行它,但它无效。

def setup_actions_controllers_db

  write_permission("all", "manage", "Everything", "All operations", true)

  controllers = Dir.new("#{Rails.root}/app/controllers").entries
  controllers.each do |controller|
    if controller =~ /_controller/
      foo_bar = controller.camelize.gsub(".rb","").constantize.new
    end
  end
  # You can change ApplicationController for a super-class used by your restricted controllers
  ApplicationController.subclasses.each do |controller|
    if controller.respond_to?(:permission)  
      clazz, description = controller.permission
      write_permission(clazz, "manage", description, "All operations")
      controller.action_methods.each do |action|
        if action.to_s.index("_callback").nil?
          action_desc, cancan_action = eval_cancan_action(action)
          write_permission(clazz, cancan_action, description, action_desc)
        end
      end
    end
  end

end

and so on ...

请帮助我。谢谢

1 个答案:

答案 0 :(得分:1)

您必须使用该代码创建rake任务并在之后运行该任务,以便填充新创建的Permission表。要创建rake任务,您需要在<newtask>.rake

中添加文件lib/tasks/<newtask>.rake