在服务中调用辅助方法

时间:2016-10-11 18:10:07

标签: ruby-on-rails helper

似乎无法从我创建的服务中调用此帮助程序方法。它们看起来像这样:

Public Function userNameBuilder(ByVal firstName As String, ByVal lastName As String) As String
    Dim newLastName As String
    Dim newUserName As String
    If (lastName >= 5) Then
        newLastName = Left(lastName, Len(lastName) - 5)
        userNameBuilder = (newLastName & Left(firstName, 1))
    ElseIf (lastName < 5 && lastName > 0) Then
        userNameBuilder = lastName & Left(firstName, 5 - (Len(lastName)))
    Else
        userNameBuilder = vbNullString
    End If
End Function

我的服务如下:

# 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

无论哪种方式都没有运气。我哪里误入歧途?

1 个答案:

答案 0 :(得分:1)

需要调用控制器的控制器方法 ,这意味着您已在错误的位置中定义了此方法。

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

他说,为了让你的尝试有效:

ApplicationController.new.activate_session_with_id(domain, token)