名称空间中的devise_scope,rails api_only模式

时间:2018-08-18 11:36:26

标签: ruby-on-rails devise devise-confirmable

我试图在api_only rails设置中删除过时的设计路线。但是我对如何使用devise_scope正确定义它们大惊小怪。我有以下route.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

其中指的是包含自定义json呈现器而不是典型的response_with的confirmations_controller:

# 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映射。但是我不确定如何正确解决此问题,而不必在每个devise控制器中都包含绑定。从路线上可以吗?

1 个答案:

答案 0 :(得分:0)

我从未尝试过在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