[英]AngularJS Rails User Sign Up without Devise
我正在嘗試使用Angular客戶端通過$ resource服務將用戶輸入發送到服務器來創建注冊流程。 Rails側的User模型在模型上具有has_secure_password和password_digest字段。
問題是:(1)即使沒有,Rails也會向Angular客戶端發送“缺少密碼”的錯誤。 (2)Angular似乎沒有發送密碼和確認信息? (3)密碼摘要未設置,保持為“ nil”。
(1)即使沒有,Rails也會向Angular客戶端發送“缺少密碼”的錯誤。
Object { username: "testuser", first_name: "Test", last_name: "User", email: "test@sample.com", 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"=>"user3@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user"=>{"username"=>"user3", "first_name"=>"Uss", "last_name"=>"ErThree", "email"=>"user3@example.com"}}
Can't verify CSRF token authenticity
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'user3@example.com' 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", "user3@example.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: "user3@example.com", 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
結束
我想您的Rails控制器不允許在密碼和密碼確認中傳遞密碼,因為不允許在強參數中使用密碼和密碼確認。 要么就是您不將它們包括在要發布的json用戶對象中。 由於您很可能正在執行User.create!(params [:user]),因此需要確保已允許所有期望的參數:
permitted_params = params[:user].permit(:username, :first_name, :last_name, :email, :password, :password_confirmation)
User.create!(permitted_params)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.