devise_scope 命名空间,rails api_only 模式

devise_scope in namespaces, rails api_only mode

我试图在我的 api_only rails 设置中删除过时的设计路线。然而,我对如何使用 devise_scope 正确定义它们感到困惑。我有以下 routes.rb:

# config/routes.rb

Rails.application.routes.draw do    
  namespace :api do
    namespace :users do
      devise_scope :user do
        resource :confirmations, only: %i[create show], format: false
      end
    end
  end
end

指的是 confirmations_controller 包含自定义 json 呈现而不是典型的 respond_with:

# app/controllers/api/users/confirmations_controller.rb

module Api
  module Users
    class ConfirmationsController < Devise::ConfirmationsController
      # POST /resource/confirmation
      def create
        self.resource = resource_class.send_confirmation_instructions(resource_params)
        yield resource if block_given?

        if successfully_sent?(resource)
          # respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
          render json: { status: 201 }, status: :created
        else
          # respond_with(resource)
          render json: { status: 422, errors: resource.errors.keys },
                 status: :unprocessable_entity
        end
      end

      # GET /resource/confirmation?confirmation_token=abcdef
      def show
        self.resource = resource_class.confirm_by_token(params[:confirmation_token])
        yield resource if block_given?

        if resource.errors.empty?
          set_flash_message!(:notice, :confirmed)
          # respond_with_navigational(resource) { redirect_to after_confirmation_path_for(resource_name, resource) }
          render json: { status: 200 }, status: :ok
        else
          # respond_with_navigational(resource.errors, status: :unprocessable_entity) { render :new }
          render json: { status: 422, errors: resource.errors.keys },
                 status: :unprocessable_entity
        end
      end
    end
  end
end

如 routes.rb 中所示,我只需要创建和显示确认端点。但是,当 运行 rspec:

时,当前定义会导致以下错误
 Failure/Error: get api_users_confirmations_path, params: { confirmation_token: 'incorrect_token'  }

 AbstractController::ActionNotFound:
   Could not find devise mapping for path "/api/users/confirmations?confirmation_token=incorrect_token".
   This may happen for two reasons:

   1) You forgot to wrap your route inside the scope block. For example:

     devise_scope :user do
       get "/some/route" => "some_devise_controller"
     end

   2) You are testing a Devise controller bypassing the router.
      If so, you can explicitly tell Devise which mapping to use:

      @request.env["devise.mapping"] = Devise.mappings[:user]

考虑到 devise_scope 的定义正确,这主要是由于缺少设计映射。但是我不确定如何在不必在每个设计控制器中包含绑定的情况下正确解决这个问题。这在路线上可行吗?

我从未尝试使用 devise_scope 中的资源。

我是这样定义的。

devise_scope :user do
  delete 'logout', to: 'devise/sessions#destroy'
end

这是我在我的一个应用程序中定义的方式

    devise_for :users, path: 'api/v1/accounts', controllers: {
        :registrations      => 'api/v1/accounts/registrations',
        :sessions           => 'api/v1/accounts/sessions',
        :passwords          => 'api/v1/accounts/passwords'
      }

devise_scope :user do
    get  '/sessions/new' => "sessions#new", :as => :new_sessions
    get  '/sessions/forgot_password' => "sessions#forgot_password", :as => :forgot_password
    post '/validate_referral_code' => 'validates#validate_referral_code', as: :validate_referral_code
    post '/validate_employment_code' => 'validates#validate_employment_code', as: :validate_employment_code
    post '/get_weather' => 'temperature#weather', as: :weather
    get '/fetch' => 'zip_codes#fetch', as: :fetch_zip_code
    post '/request_demo' => 'demos#create', as: :create
  end

namespace :api do
    namespace :v1 do
      scope :accounts do
        resources :third_party_logins, only: [] do
          collection do
            get :action_name
          end
        end
      end
    end
end