我使用devise_token_authentication
gem来构建基于令牌的身份验证rails api,然后我通过不同的迁移向Vendor
模型添加了一些额外的字段,并且为了允许它们我写了这样:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :tax_number])
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :tax_number])
end
end
然后我添加了另一个模型Customer
rails g devise_token_auth:install Customer auth
然后在routes.rb
Rails.application.routes.draw do
mount_devise_token_auth_for 'Vendor', at: 'vendor/auth'
mount_devise_token_auth_for 'Customer', at: 'customer/auth'
end
每次我通过sign_up
向客户'localhost:3000/customer/auth'
尝试时收到错误消息:ActiveModel::UnknownAttributeError: unknown attribute 'tax_number' for Customer.
那么有没有办法只允许Vendor
模型的额外字段,并跳过&#39;客户&#39; ?
答案 0 :(得分:1)
查看this setup for multiple devise user models.
或
如果您覆盖RegistrationsController
,则需要在registrationsController
中直接允许额外的参数
class Users::RegistrationsController < DeviseTokenAuth::RegistrationsController
def create
end
def account_update
end
private
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name, :tax_number)
end
end