简体   繁体   English

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

[英]NameError (uninitialized constant ShopsController::ShopService - Ruby on Rails

I am getting this error, don't know why.我收到这个错误,不知道为什么。

NameError (uninitialized constant ShopsController::ShopService

My controller name is ShopsController I've made a service /app/services/shop_service.rb The name of the class inside the service is ShopService I'm using it inside a controller action the following way:我的控制器名称是ShopsController我已经创建了一个服务/app/services/shop_service.rb服务内部的类的名称是ShopService我在控制器操作中使用它的方式如下:

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

The service code is written below服务代码写在下面

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

Try to call the service like that: flag = ::ShopService.new.save_categories(@shop, params[:category]) on the controller, this should work尝试像这样调用服务: flag = ::ShopService.new.save_categories(@shop, params[:category])在控制器上,这应该可以工作

Edit:编辑:

Also, check that you are loading the services path in the config/application.rb , on the config.autoload_paths另外,请检查您是否正在config.autoload_paths上的config/application.rb中加载服务路径

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

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