我正在尝试通过Angular客户端通过$ resource服务将用户输入发送到服务器来创建注册流。 Rails端的User模型在模型上有一个has_secure_password,password_digest字段。AngularJS Rails用户无需注册
问题是: (1)Rails向Angular客户端发送“密码丢失”错误,即使它不是。 (2)密码和确认似乎不会被Angular发送? (3)密码摘要未设置,仍然为“无”。
(1)Rails向Angular客户端发送“密码丢失”错误,即使它不是。
Object { username: "testuser", first_name: "Test", last_name: "User", email: "[email protected]", password: "password5375", password_confirmation: "password5375" } main.js:157
Object { data: Object, status: 422, headers: headersGetter/<(), config: Object, statusText: " " } main.js:163
"password:can't be blank"
(2)请注意,密码&确认不包括在包装的“用户”散列
Started POST "/api/users.json" for 127.0.0.1 at 2014-10-04 19:04:46 -0400
Processing by UsersController#create as JSON
Parameters: {"username"=>"user3", "first_name"=>"Uss", "last_name"=>"ErThree", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user"=>{"username"=>"user3", "first_name"=>"Uss", "last_name"=>"ErThree", "email"=>"[email protected]"}}
Can't verify CSRF token authenticity
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = 'user3' LIMIT 1
SQL (164.1ms) INSERT INTO "users" ("created_at", "email", "first_name", "last_name", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Sat, 04 Oct 2014 23:04:46 UTC +00:00], ["email", "[email protected]xample.com"], ["first_name", "Uss"], ["last_name", "ErThree"], ["updated_at", Sat, 04 Oct 2014 23:04:46 UTC +00:00], ["username", "user3"]]
(0.8ms) commit transaction
(3)从控制台,注意密码摘要是没有得到设置。
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 3, created_at: "2014-10-04 23:04:46", updated_at: "2014-10-04 23:04:46", username: "user3", first_name: "Uss", last_name: "ErThree", email: "[email protected]", password_digest: nil>
1.9.3-p362 :010 > User.last.password_digest
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> nil
app.factory('User', function($resource){
function User() {
this.service = $resource('/api/users/:id.json', {id: '@id'},
{
'update': {method: 'PUT'},
'show': {method: 'GET', isArray: false}
}
);
}
User.prototype.all = function() {
return this.service.query();
};
User.prototype.destroy = function(uid) {
return this.service.remove({id: uid});
};
User.prototype.create = function(attributes) {
return this.service.save(attributes);
};
User.prototype.update = function(attributes) {
return this.service.update(attributes);
};
User.prototype.show = function(uid) {
return this.service.show({id: uid});
}
return new User;
});
app.controller('UsersCreateCtrl', function($scope, User){
$scope.createUser = function(){
$scope.user;
console.log($scope.user);
var newUser = User.create($scope.user).$promise.then(function(data){
console.log(data);
$scope.users.push(newUser);
$scope.user = {};
}, function(msg){
console.log(msg);
angular.forEach(msg.data.errors, function(type, error){
console.log(error + ":" + type);
});
});
};
});
<h4>Register</h4>
<div ng-controller="UsersCreateCtrl">
<form name="form" ng-submit="createUser()" novalidate>
Name: <input name="username" type="text" ng-model="user.username" placeholder="username"/>
First Name: <input name="first_name" type="text" ng-model="user.first_name" placeholder="first name" />
Last Name: <input name="last_name" type="text" ng-model="user.last_name" placeholder="last name" />
Email: <input name="email" type="text" ng-model="user.email" placeholder="email" />
Password: <input name="password" type="text" ng-model="user.password" placeholder="password" />
Confirm Password: <input name="password_confirmation" type="text" ng-model="user.password_confirmation" />
<input type="submit" value="Register">
</form>
</div>
编辑:Rails的UsersController发布
class UsersController < ApplicationController
respond_to :json
protect_from_forgery with: :null_session
def index
respond_with User.all
end
def create
respond_with User.create(user_params)
end
def destroy
respond_with User.destroy(params[:id])
end
def show
respond_with User.find(params[:id])
end
def update
respond_with User.find(params[:id]).update_attributes(source_params)
end
private
def user_params
params.require(:user).permit(:username, :email, :first_name, :last_name, :password, :password_confirmation)
end
末
发表您的轨道控制器 – user2936314 2014-10-05 01:30:15