首页 > 解决方案 > NameError(未初始化的常量 Api::V1::RegistrationsController::JsonWebTokenAuthentication):

问题描述

我的 jsonwebtokenauthentication.rb 位于(app/lib.jsonwebtokenauthentication.rb):

class JsonWebTokenAuthentication
 def some_method
    #logic of the method
 end
end

我正在尝试JsonWebTokenAuthentication在我的 registrations_controller.rb(app/controllers/api/v1/registrations_controller.rb) 中访问上述方法

class Api::V1::RegistrationsController < Api::V1::BaseController

  def create
      auth_token = JsonWebTokenAuthentication.some_method({user_id: user.id})
    end
  end
end

我们如何使用在 rails 项目的 lib 文件夹中指定的类方法。

标签: ruby-on-rails

解决方案


首先,如果你想使用 RoR 框架,你应该使用Ruby Style guide来命名你的文件。也JsonWebTokenAuthentication看起来更像 a module,而不是 a class,你能澄清你为什么选择class这里吗?

我建议将其添加some_methodApplicationController。另一种选择是将 a 添加json_web_token_authentication.rbapp/services/但使用与控制器相同的命名空间。


推荐阅读