我正在尝试通过API调用设置可确认的用户注册。如果我不启用可确认,它会很好地创建用户,但是如果我这样做,我会收到以下错误:
No route matches {:action=>"show", :confirmation_token=>"hidden", :controller=>"api/users/confirmations"}, missing required keys: [:format]
我的猜测是它需要一些东西来指定 JSON 以进行确认,但我不知道该怎么做。
我已经在确认控制器中指定接受 JSON:
class Api::Users::ConfirmationsController < Devise::ConfirmationsController
skip_before_action :verify_authenticity_token
respond_to :json
end
这是我的注册控制器:
class Api::Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
skip_before_action :verify_authenticity_token
respond_to :json
# POST /resource
def create
super
end
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :password, :first_name, :last_name])
end
end
以下是我的路线:
Rails.application.routes.draw do
scope :api, "/1", module: "api", :format => true do
devise_for :users, :controllers => {registrations: 'api/users/registrations', passwords: "api/users/passwords", confirmations: "api/users/confirmations"}
post 'auth_user' => 'authentication#authenticate_user'
devise_scope :user do
post 'password/forgot', to: 'users/passwords#forgot'
post 'password/reset', to: 'users/passwords#reset'
end
end
end
这是我所做的 API 调用,它是对 api/1/users.json 的 POST
{
"user": {
"first_name": "Dev",
"last_name": "Dude",
"email": "dev@null.com",
"password": "!1Qwerty"
}
}
我想
通了,我只需要强制设计始终在路由中将格式设置为 JSON,而不仅仅是在控制器中设置respond_to:
这是我在路线中更改的线路。请注意添加的默认值参数:
devise_for :users, :controllers => {registrations: 'api/users/registrations', passwords: "api/users/passwords", confirmations: "api/users/confirmations"}, :defaults => { :format => :json }