ruby-on-rails - 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 文件夹中指定的类方法。
解决方案
首先,如果你想使用 RoR 框架,你应该使用Ruby Style guide来命名你的文件。也JsonWebTokenAuthentication
看起来更像 a module
,而不是 a class
,你能澄清你为什么选择class
这里吗?
我建议将其添加some_method
到ApplicationController
。另一种选择是将 a 添加json_web_token_authentication.rb
到app/services/
但使用与控制器相同的命名空间。
推荐阅读
- python - python3列表之间的共享内存
- android-studio - 导航到其他片段时保持 viewpager2 项目位置
- python - 使用序列加入 Pandas
- reactjs - 无法从 React 前端获取到 Expo 推送通知 api
- python - 使用 Scipy 的非凸函数优化问题。不是最初的猜测
- codeeffects - 元素
从 XML 实例化规则后,GetRuleXml() 中的方法名称不正确 - .net-core - .NET Core 3.1 中的默认代理
- ios - UITextFieldDelegate:如何选择为每个单独的 TextField 调用哪些方法
- python - 如何在python中使用正则表达式在字符串列表中查找单词
- sql - 试图将变量从熊猫传递给sql