为什么我的服务未正确声明?

时间:2018-10-01 21:47:59

标签: ruby-on-rails stripe-payments

我正在使用Stripe为我的应用创建付款服务。我有:

#app/services/service_error.rb

class PaymentGateway::ServiceError < StandardError
  attr_reader :exception_message

  def initialize(message, exception_message: )
    # Call the parent's constructor to set the message
    super(message)

    # Store the exception_message in an instance variable
    @exception_message = exception_message
  end
end

class PaymentGateway::CreateSubscriptionServiceError < PaymentGateway::ServiceError
end

在我的控制器中,我试图运行它:

#app/controller/subscriptions_controller.rb

class SubscriptionsController < ApplicationController
  rescue_from PaymentGateway::CreateSubscriptionServiceError do |e|
    redirect_to root_path, alert: e.message
  end

当我运行代码时,我收到此错误:

ActionController::RoutingError (uninitialized constant PaymentGateway::CreateSubscriptionServiceError):

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

正确的路径如下所示:

# app/services/payment_gateway/service_error.rb
class PaymentGateway::ServiceError < StandardError
end

# app/services/payment_gateway/create_subscription_service_error.rb
class PaymentGateway::CreateSubscriptionServiceError < PaymentGateway::ServiceError
end

ActiveSupport然后应该能够正确加载类。