ruby-on-rails - 如何使用 angularjs 和 rails/devise 更新密码?

标签 ruby-on-rails angularjs devise

我在尝试使用包含旧密码和新密码的简单表单更新用户密码时遇到问题。问题是我将 {"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/

相关文章:

javascript - 尝试替换 $location.search 时如何避免转义?

javascript - 无法读取 'undefined' 的属性推送

javascript - Uncaught Error : [$injector:modulerr] Failed to instantiate module scheduleApp due to:

ruby-on-rails - 设计:突然无法登录

ruby-on-rails - 如何使用 linkedin gem 和 omniauth 获取更详细的 LinkedIn 用户信息?

ruby-on-rails - 使用 RSPec 模拟/ stub 系统调用

ruby-on-rails - 使用 Rails find_or_initialize_by_email,如何确定记录是否已找到或已初始化?

ruby-on-rails - Rails、Devise、Validatable 不起作用?

ruby-on-rails - 移动浏览器关闭时保持用户登录(Rails)

ruby-on-rails - 测试 Rails 查询计数