Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为自定义行为添加类方法_Ruby On Rails_Ruby On Rails 4_Activesupport Concern - Fatal编程技术网

Ruby on rails 为自定义行为添加类方法

Ruby on rails 为自定义行为添加类方法,ruby-on-rails,ruby-on-rails-4,activesupport-concern,Ruby On Rails,Ruby On Rails 4,Activesupport Concern,我想在调用acts\u asmethod时动态添加类方法。我的文件结构如下所示(取自): 我只想在类调用充当\u foo时才能声明类方法。实例方法与LocalInstanceMethods一起使用。最简单的方法是将插件添加到模块中,但我的其他所有类也都有此方法。我已经尝试将define\u方法添加到acts\u as方法中,返回未定义的方法。有什么想法吗?我认为您只需要创建第二个子模块,并使用extend而不是include module ActsAsFoo extend ActiveSup

我想在调用
acts\u as
method时动态添加类方法。我的文件结构如下所示(取自):


我只想在类调用
充当\u foo
时才能声明类方法。实例方法与
LocalInstanceMethods
一起使用。最简单的方法是将插件添加到模块中,但我的其他所有类也都有此方法。我已经尝试将
define\u方法
添加到
acts\u as
方法中,返回未定义的方法。有什么想法吗?

我认为您只需要创建第二个子模块,并使用
extend
而不是
include

module ActsAsFoo
  extend ActiveSupport::Concern

  included do
  end

  module ClassMethods
    def acts_as_foo(options = {})
      extend ActsAsFoo::LocalClassMethods
    end
  end

  module LocalClassMethods
    def class_method
      # class method definition
    end
  end
end

class Bar
  acts_as_foo
end

是否要将类方法或实例方法添加到
?你的问题是类方法,但模块名为
LocalInstanceMethods
@Slicedpan我不清楚。我重写了我的问题。我可以添加本地实例,但不能添加类方法。
module ActsAsFoo
  extend ActiveSupport::Concern

  included do
  end

  module ClassMethods
    def acts_as_foo(options = {})
      extend ActsAsFoo::LocalClassMethods
    end
  end

  module LocalClassMethods
    def class_method
      # class method definition
    end
  end
end

class Bar
  acts_as_foo
end