【问题标题】:NameError (uninitialized constant Api::V1::RegistrationsController::JsonWebTokenAuthentication):NameError(未初始化的常量 Api::V1::RegistrationsController::JsonWebTokenAuthentication):
【发布时间】:2018-05-27 20:48:51
【问题描述】:

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

class JsonWebTokenAuthentication
 def some_method
    #logic of the method
 end
end

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

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


【解决方案1】:

首先,如果您想使用 RoR 框架,您应该使用 Ruby Style guide 命名您的文件。另外JsonWebTokenAuthentication 看起来更像module,对我来说不是class,你能澄清一下你为什么在这里选择class 吗?

我建议将some_method 添加到ApplicationController。另一种选择是将json_web_token_authentication.rb 添加到app/services/,但使用与控制器相同的命名空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多