ruby-on-rails - simple_token_authentication - 未定义的方法`authenticate_user
问题描述
我正在使用devise和simple_token_authentication创建一个支持令牌身份验证的 API
我已按照指南中的所有步骤进行操作:
# app/models/user.rb
class User < ApplicationRecord
acts_as_token_authenticatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::API
acts_as_token_authentication_handler_for User
end
当我尝试到达路线“/books”以列出我的模型书的所有条目时,我收到以下错误:
<NoMethodError:
undefined method `authenticate_user!' for
# <BooksController:0x0000563e7bb26820>
Did you mean? authenticate_user_from_token!
>
我做错了什么?
更新
更多信息:
$ rails routes
Prefix Verb URI Pattern Controller#Action
books GET /books(.:format) books#index
POST /books(.:format) books#create
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
sessions POST /sessions(.:format) sessions#create
GET /books/nearby/:latitude/:longitude/:distance(.:format) books#nearby
更新2
如果有帮助,我已将整个回购公开。查看分支simple_token_auth https://bitbucket.org/enmotent/bookaneer-api/src/master/
解决方案
尝试
class ApplicationController < ActionController::API
acts_as_token_authentication_handler_for User, fallback: :none
end
class BooksController < ApplicationController
before_action :requires_login
end
private
def requires_login
unless current_user
render json: { errors: "This requires login" }
end
end
end
推荐阅读
- python - 逆矩阵结果在 MATLAB 和 Python 中不同
- laravel - 通过 .htaccess / 异常管理位置 / 框架 Laravel 重定向到安全连接
- c# - 在 C# 中使用 Git TFS API 在一次提交中进行移动和编辑?
- stanford-nlp - 如何从文件中读取规则
- android - 如何将 Android Studio 项目导出到特定存储库中的新 GitHub 分支?
- java - 无法将功能作为内容添加到 Vogella Eclipse RCP 教程项目
- javascript - 为什么没有导入文件 - 用 Jest 测试 VueJS
- python - 如何 pip 卸载使用 git 项目 URL 安装的包?
- vba - Excel VBA/公式。用当月的每个星期五填充一系列单元格
- python - 使用 cuda 和浮点张量的问题