gpt4 book ai didi

ruby-on-rails - 未找到 authenticate_with_http_token 方法

转载 作者:数据小太阳 更新时间:2023-10-29 07:43:53 25 4
gpt4 key购买 nike

我正在用 ruby​​ on rails 制作一个 api,我正在尝试使用基于 token 的身份验证。一切正常,但 Rails 说 authenticate_with_http_token 方法未定义。

这是它给出的错误:

{"status":500,"error":"Internal Server Error","exception":"#\u003cNoMethodError: undefined method `authenticate_with_http_token' for #\u003cUsersController:0x007fa8ac16dee0\u003e\u003e","traces":{"Application Trace":[{"id":0,"trace":"app/controllers/users_controller.rb:59:in `authenticate_token'"},{"id":1,"trace":"app/controllers/users_controller.rb:55:in `authenticate'"}],"Framework Trace":[{"id":2,"trace":"activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:382:in `block in make_lambda'"}

这是我的 Controller 的代码:

class UsersController < ApplicationController

before_action :set_user, only: [:show, :update, :destroy]
before_action :authenticate, only: [:show, :update, :destroy]

# GET /users
def index
@users = User.all

render json: @users
end

# GET /users/1
def show
render json: @user
end

# POST /users
def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /users/1
def destroy
@user.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :school_id, :auth_token, :password_digest)
end

def authenticate
authenticate_token || render_unauthorized
end

def authenticate_token
authenticate_with_http_token { |token, options| User.find_by(auth_token: token) }
end
end

最佳答案

尝试将其包含在您的 ApplicationController 或 UsersController 中

include ActionController::HttpAuthentication::Token::ControllerMethods

关于ruby-on-rails - 未找到 authenticate_with_http_token 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35839803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com