简体   繁体   English

Ruby on Rails 6 加载模块

[英]Ruby on Rails 6 loading module

I would like to know what is the right way to use a class with namespace in rails 6. I have the follow, but it isn't working and I'm receiving the error:我想知道在 rails 6 中使用带有命名空间的类的正确方法是什么。我有以下内容,但它不起作用,我收到了错误:

"Uninitialized constant ProductsController::Operations Did you mean? ProductsController::Options" “未初始化的常量 ProductsController::Operations 你的意思是?ProductsController::Options”

#app/operations/create.rb
module Operations
  class Create
    def self.foo
      ...
    end
  end
end

#app/controllers/products_controller.rb

class ProductsController < ApplicationController
  def create
    Operations::Create.foo
  end
end

Can you help me please?你能帮我吗?

Your module should be inside of any folder.您的模块应该在任何文件夹内。 For example app/services/operations/create.rb (any name will work) with the same content you have:例如app/services/operations/create.rb (任何名称都可以)与您拥有的内容相同:

module Operations
  class Create
    def self.foo
      ...
    end
  end
end

and call it Operations::Create.foo .并将其称为Operations::Create.foo

Also make sure you restart spring with spring stop .还要确保用spring stop重新启动 spring 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM