【发布时间】:2017-01-11 11:54:17
【问题描述】:
/auth/sign_up 上的 GET 未按预期运行。收到以下 404 错误
{
"status": 404,
"error": "Not Found",
"exception": "#<AbstractController::ActionNotFound: The action 'new' could not be found for DeviseTokenAuth::RegistrationsController>",
"traces": #too long to post; 46 traces; none includes user created files
}
这就是我所做的一切
-
创建了一个新的 Rails API 项目
rails new untitled --javascript=jquery --api -
将此添加到
Gemfile#authentication gem 'devise' gem 'omniauth' gem 'devise_token_auth' -
运行以下
bundle install rails generate devise_token_auth:install User auth rails db:migrate rails s 从浏览器和
Postman测试了网址localhost:3000/auth/sign_up
我的路线.rb
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
end
我的 rails 路线输出
Prefix Verb URI Pattern Controller#Action
new_user_session GET /auth/sign_in(.:format) devise_token_auth/sessions#new
user_session POST /auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
user_password POST /auth/password(.:format) devise_token_auth/passwords#create
new_user_password GET /auth/password/new(.:format) devise_token_auth/passwords#new
edit_user_password GET /auth/password/edit(.:format) devise_token_auth/passwords#edit
PATCH /auth/password(.:format) devise_token_auth/passwords#update
PUT /auth/password(.:format) devise_token_auth/passwords#update
cancel_user_registration GET /auth/cancel(.:format) devise_token_auth/registrations#cancel
user_registration POST /auth(.:format) devise_token_auth/registrations#create
new_user_registration GET /auth/sign_up(.:format) devise_token_auth/registrations#new
edit_user_registration GET /auth/edit(.:format) devise_token_auth/registrations#edit
PATCH /auth(.:format) devise_token_auth/registrations#update
PUT /auth(.:format) devise_token_auth/registrations#update
DELETE /auth(.:format) devise_token_auth/registrations#destroy
user_confirmation POST /auth/confirmation(.:format) devise_token_auth/confirmations#create
new_user_confirmation GET /auth/confirmation/new(.:format) devise_token_auth/confirmations#new
GET /auth/confirmation(.:format) devise_token_auth/confirmations#show
auth_validate_token GET /auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
auth_failure GET /auth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /auth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#omniauth_success
GET|POST /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
omniauth_failure GET|POST /omniauth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /auth/:provider(.:format) redirect(301)
我注意到的其他事情
- 其他路线,例如。
/auth/sign_in工作完美,我能够为我使用Rails Console创建的用户成功登录 看起来类似的错误导致了这个post,但不是由于routes.rb中的subdomain constraints造成的答案中给出的并没有摆脱 404
这就是我当时尝试的方法
- 试过
rails generate devise:controller users -
RegistrationsController中的new部分未注释 - 添加了一些随机的
render :jsons,它总是抛出一个空的 JSON,状态为 200,终端上出现奇怪的bin/rails: No such file or directory错误 - 尝试使用
rails app:update:bin摆脱它,没有任何变化:(
我做错了什么?
【问题讨论】:
-
DeviseTokenAuth::RegistrationsController没有新的操作,当您考虑到基于令牌的身份验证主要用于 API 或单页应用程序时,这很有意义,其中呈现新路由是在 javascript 中完成的,而不是在铁轨上。 -
一般来说,您在 API 应用程序中没有
new或edit操作,因为您不会呈现要在 Web 界面中使用的表单。对于 API 客户端GET /some_resource/new完全没有意义。 -
公共 API 怎么样?他们如何告诉通过 API 而不是通过文档注册新用户需要什么
标签: ruby-on-rails routing ruby-on-rails-5 rails-api