我在尝试使用包含旧密码和新密码的简单表单更新用户密码时遇到问题。问题是我将 {"user[current_password]":$scope.user.password,"user[password]":$scope.user.newpassword}
数据发布到我的 Devise::RegistrationsController
并且我得到一个简单的 422 Unprocessable Entity
响应。我哪里错了?
这是我的html
<div ng-controller="PasswordCtrl">
<form id="password-card" class="row" style="display:none;" name="password_form" ng-submit="changePassword()" novalidate>
<div class="span5">
<div class="form-inputs">
<label for="nPass"><%= I18n.t("registration.edit.current_password") %></label>
<input name="uPass" class="required" type="password" ng-model="user.password" required >
<div ng-show="password_form.uPass.$dirty && password_form.uPass.$invalid">Missing:
<span ng-show="password_form.uPass.$error.required">You must provided your current password.</span>
</div>
<label for="nPass"><%= I18n.t("registration.edit.new_password") %></label>
<input name="nPass" class="required" type="password" ng-model="user.newpassword" required >
<div ng-show="password_form.nPass.$dirty && password_form.nPass.$invalid">Missing:
<span ng-show="password_form.nPass.$error.required">Tell us your new password.</span>
</div>
<button id="x" type="submit" ng-disabled="!password_form.$valid" class="button secondary prefix">Breyta</button>
</div>
</div>
</form>
</div>
这是我的 Angular Controller
var PasswordCtrl=function($scope,registrationService, $http){
$scope.changePassword=function(){
console.log("current_password",password_form.uPass.value);
$http({
method: 'PUT',
url: '/skraning.json',
headers: {'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')},
data: {"user[current_password]":$scope.user.password,"user[password]":$scope.user.newpassword}
}).
success(function(data, status, headers, config) {
console.log("done",data);
});
};
};
最佳答案
你传递的数据对象无效(user[current_password] & user[password]):用引号尝试这样
数据:{"user['current_password']":$scope.user.password,"user['password']":$scope.user.newpassword}
关于ruby-on-rails - 如何使用 angularjs 和 rails/devise 更新密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21501604/