0

search_handlers_controller.rb

class SearchHandlersController < ApplicationController
  def match 
    @string = 'string'        
    @result = case params['search_style']
    when 'match'
      MatchService.call(@string)    
      ...     

/services 中的 MatchService.rb:

# frozen_string_literal: true
class MatchService < ApplicationService
  ...
  def call(string)
    'Match'
  end
end

从 SearchHandlersController#match 调用 MatchService 时出错:

NameError (uninitialized constant SearchHandlersController::MatchService):
app/controllers/search_handlers_controller.rb:18:in `match'
4

1 回答 1

1

首先,如果您将文件命名为MatchService.rb,则应将此文件的名称更改为match_service.rb.

如果这在您的情况下不起作用,您始终可以通过::在服务名称之前添加如下方式从根目录调用服务:::MatchService.call(@string).

我希望它对你有帮助!

于 2019-11-19T16:30:58.437 回答