【发布时间】:2018-08-18 11:36:25
【问题描述】:
我正在尝试在我的 api_only rails 设置中删除过时的设计路线。但是,我对如何使用 devise_scope 正确定义它们大惊小怪。我有以下路线.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
指的是confirms_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 定义正确,这主要是由于缺少设计映射。但是,我不确定如何正确解决此问题,而不必在每个设计控制器中都包含绑定。这在路线上可行吗?
【问题讨论】:
标签: ruby-on-rails devise devise-confirmable