【问题标题】:Rails 5 AbstractController::ActionNotFound: The action 'new' could not be found for DeviseTokenAuth::RegistrationsController on [GET] "/auth/sign_up"Rails 5 AbstractController::ActionNotFound:在 [GET] "/auth/sign_up" 上找不到 DeviseTokenAuth::RegistrationsController 的操作“新”
【发布时间】: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 摆脱它,没有任何变化:(

我做错了什么?

【问题讨论】:

  • 可能bug 在 token-auth gem 上,但它已经被提出并且它的状态现在已经关闭 他们似乎试图删除这条路线,但由于某些原因让它留下来声明here
  • DeviseTokenAuth::RegistrationsController 没有新的操作,当您考虑到基于令牌的身份验证主要用于 API 或单页应用程序时,这很有意义,其中呈现新路由是在 javascript 中完成的,而不是在铁轨上。
  • 一般来说,您在 API 应用程序中没有 newedit 操作,因为您不会呈现要在 Web 界面中使用的表单。对于 API 客户端 GET /some_resource/new 完全没有意义。
  • 公共 API 怎么样?他们如何告诉通过 API 而不是通过文档注册新用​​户需要什么

标签: ruby-on-rails routing ruby-on-rails-5 rails-api


【解决方案1】:

devise_token_auth 的 README 文档中所述

如果这个 gem 不使用新路由,为什么还要包含它们?

删除新路线需要对以下内容进行重大修改 设计。如果包含新路线导致您的应用出现任何 问题,在问题跟踪器中发布问题,它将得到解决 尽快。

newedit 功能不由 devise_token_auth 提供,但路由仍会显示它们,因为路由来自 devisedevise_token_auth 是基于这些路由构建的,并且很难撤消这些路由。

通常,对于基于 API 的应用程序,您并不需要该功能,因为该逻辑内置于 API 客户端(例如:angular.js 或 react.js)。如果由于某种原因,您需要让 API 客户端知道 createupdate 所需的数据,您可以记录它或者当 API 客户端提供错误数据时,您可以提供必填字段信息作为错误。

【讨论】:

  • 如果没有提供new功能怎么注册?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多