RubyonRails

z6psavjg  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(200)

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

NameError (uninitialized constant ShopsController::ShopService

我的控制器名为shopscontroller我提供了服务 /app/services/shop_service.rb 服务中类的名称为 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
beq87vna

beq87vna1#

尝试这样呼叫服务: flag = ::ShopService.new.save_categories(@shop, params[:category]) 在控制器上,这应该可以工作
编辑:
此外,请检查您是否正在中加载服务路径 config/application.rb ,在 config.autoload_paths

相关问题