2016-10-11 22 views
2

似乎无法从我创建的服务中调用此辅助方法。他们是这样的:在服务中调用辅助方法

# 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 

我也试着拨打helper方法直线上升,如下所示:

activate_shopify_session_with_id(@domain, shop_token) 

N运气无论哪种方式。我在哪里误入歧途?

回答

1

呼叫控制器的方法的需要外界控制器的意味着你已经在错误的地方定义此方法。

您可以将该方法移动到某个辅助模块并在两个地方使用(通过包含它)。

这是说,让你的工作的尝试:

ApplicationController.new.activate_session_with_id(domain, token) 
+0

好,感谢suggestion.Ahh,所以如果我创建了这个方法的模块,我只想包括服务的模块,并调用方法根据需要?是? – ToddT

+0

@ToddT是的,你会包括它在任何类,使用这种方法:) –