Ruby on rails 在名称空间中设计\u作用域,rails api \u-only模式
在我的api_-only rails设置中,我试图删除Desive的过时路线。然而,我对如何使用design_范围正确定义它们感到困惑。我有以下路线。rb:Ruby on rails 在名称空间中设计\u作用域,rails api \u-only模式,ruby-on-rails,devise,devise-confirmable,Ruby On Rails,Devise,Devise Confirmable,在我的api_-only rails设置中,我试图删除Desive的过时路线。然而,我对如何使用design_范围正确定义它们感到困惑。我有以下路线。rb: # config/routes.rb Rails.application.routes.draw do namespace :api do namespace :users do devise_scope :user do resource :confirmations, only: %i[
# 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呈现的确认\u控制器,而不是典型的带有以下内容的响应\u控制器:
# 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
考虑到正确定义了设计范围,这主要倾向于缺少设计映射。但是,我不知道如何正确地解决这个问题,而不必在每个Desive控制器中都包含绑定。从路线上看,这可行吗?我从未尝试使用Desive\u范围内的资源 这就是我对它的定义
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
这里需要注意的是,designe作用域是在api的名称空间之外定义的,它将与designe资源名称空间处理产生冲突!谢谢你的回答@Kedarnag!
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