我正在嘗試使用本教程創建一個小專案來練習 API 密鑰身份驗證。我在 rails 專案的以下目錄中創建了一個問題。
應用程式/控制器/關注/api_key_authenticable.rb
module ApiKeyAuthenticatable
extend ActiveSupport::Concern
include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionController::HttpAuthentication::Token::ControllerMethods
attr_reader :current_api_key
attr_reader :current_bearer
# Use this to raise an error and automatically respond with a 401 HTTP status
# code when API key authentication fails
def authenticate_with_api_key!
@current_bearer = authenticate_or_request_with_http_token &method(:authenticator)
end
# Use this for optional API key authentication
def authenticate_with_api_key
@current_bearer = authenticate_with_http_token &method(:authenticator)
end
private
attr_writer :current_api_key
attr_writer :current_bearer
def authenticator(http_token, options)
@current_api_key = ApiKey.find_by token: http_token
current_api_key&.bearer
end
end
在我的控制器中,我試圖包含這樣的問題
應用程式/控制器/ApiKeysController.rb
class ApiKeysController < ApplicationController
include ApiKeyAuthenticatable
# Require token authentication for index
prepend_before_action :authenticate_with_api_key!, only: [:index]
# Optional token authentication for logout
prepend_before_action :authenticate_with_api_key, only: [:destroy]
def index
end
def create
end
def destroy
end
end
但是當我運行服務器并訪問控制器的索引操作時,我收到以下錯誤
NameError (uninitialized constant ApiKeysController::ApiKeyAuthenticatable):
app/controllers/ApiKeysController.rb:10:in `<class:ApiKeysController>'
app/controllers/ApiKeysController.rb:2:in `<main>'
有人可以幫我解決這個問題嗎?
uj5u.com熱心網友回復:
作者在這里。您關注的檔案名是authenticable,但您的模塊是authenticatable. 您需要更正關注檔案名中的拼寫錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505032.html
標籤:轨道上的红宝石
上一篇:使用Bootstrap5.1在Rails7.0.2.3應用程式中覆寫Bootstrapmodal的關閉功能的最佳方法
