在服务中调用辅助方法
Calling helper method in services
似乎无法从我创建的服务中调用此辅助方法。它们看起来像这样:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper_method :activate_session_with_id
def activate_session_with_id(domain, token)
#do some stuff
end
end
我的服务是这样的:
# app/services/fulfillment_order.rb
class FulfillmentOrder
def create_order_user_fulfilled
shop = Shop.find_by(shopify_domain: @domain)
shop_token = shop.shopify_token
ApplicationController.helpers.activate_shopify_session_with_id(@domain, shop_token)
end
end
当我这样做时,出现了这个错误
NoMethodError (undefined method activate_shopify_session_with_id
我也试过直接调用辅助方法,像这样:
activate_shopify_session_with_id(@domain, shop_token)
两种方法都不走运。我哪里误入歧途了?
需要在控制器外部调用控制器的方法意味着你在错误的地方定义了这个方法
您可以将该方法移动到某个辅助模块并在两个地方使用(通过包含它)。
就是说,要使您的尝试奏效:
ApplicationController.new.activate_session_with_id(domain, token)
似乎无法从我创建的服务中调用此辅助方法。它们看起来像这样:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper_method :activate_session_with_id
def activate_session_with_id(domain, token)
#do some stuff
end
end
我的服务是这样的:
# app/services/fulfillment_order.rb
class FulfillmentOrder
def create_order_user_fulfilled
shop = Shop.find_by(shopify_domain: @domain)
shop_token = shop.shopify_token
ApplicationController.helpers.activate_shopify_session_with_id(@domain, shop_token)
end
end
当我这样做时,出现了这个错误
NoMethodError (undefined method activate_shopify_session_with_id
我也试过直接调用辅助方法,像这样:
activate_shopify_session_with_id(@domain, shop_token)
两种方法都不走运。我哪里误入歧途了?
需要在控制器外部调用控制器的方法意味着你在错误的地方定义了这个方法
您可以将该方法移动到某个辅助模块并在两个地方使用(通过包含它)。
就是说,要使您的尝试奏效:
ApplicationController.new.activate_session_with_id(domain, token)