为什么设计消毒剂返回空哈希

时间:2019-01-28 07:57:33

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

我正在编写仅API的Rails 5应用程序,并使用devise-jwt(和devise)gem进行用户身份验证。我正在尝试在我的自定义registrations_controller中配置强参数。在application_controller.rb中:

before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :username, :email])
end

registrations_controller.rb中:

def create
    build_resource(sign_up_params)
    resource.save
    #json response defined in application_controller.rb
    render_resource(resource)
end

这是我通过邮递员向API发出POST请求时控制台的输出:

Parameters: {"first_name"=>"john", "last_name"=>"stones", "username"=>"johnstones", "email"=>"john@stones.com"}

这是将params打印到控制台的输出:

<ActionController::Parameters {"first_name"=>"john", "last_name"=>"stones", "username"=>"johnstones", "email"=>"john@stones.com", "controller"=>"registrations", "action"=>"create"} permitted: false>

尽管有上述情况,当我执行devise_parameter_sanitizer.sanitize(:sign_up)时,我得到了一个空哈希,而上述POST引发了我为具有空白字段而设置的验证错误。请帮助我找出我所缺少的,谢谢。

(Rails 5.2 / Devise 4.2)

1 个答案:

答案 0 :(得分:2)

在您的参数哈希中缺少

Param user

{"user": {"first_name"=>"john", "last_name"=>"stones", "username"=>"johnstones", "email"=>"john@stones.com"} }