NameError(未初始化常量 ShopsController::ShopService - Ruby on Rails

NameError (uninitialized constant ShopsController::ShopService - Ruby on Rails

我收到这个错误,不知道为什么。

NameError (uninitialized constant ShopsController::ShopService

我的控制器名称是ShopsController我做了一个服务/app/services/shop_service.rb 服务里面class的名字是ShopService 我通过以下方式在控制器操作中使用它:

flag = ShopService.new.save_categories(@shop, params[:category])

服务代码写在下面

class ShopService  
  def initialize(shop = nil, services = nil); end
  def save_categories(shop, services)
    debugger
    flag = true
    services.drop(1).each do |service|
      category = Category.new(service: service, shop_id: shop.id)
      flag = false unless category.save
    end
    flag
  end
end

尝试像这样调用服务:flag = ::ShopService.new.save_categories(@shop, params[:category]) 在控制器上,这应该可以工作

编辑:

此外,请检查您是否在 config/application.rb 中加载服务路径,在 config.autoload_paths