2014-10-05 2 views
1

Angular 클라이언트가 $ resource 서비스를 통해 서버에 사용자 입력을 보내는 가입 절차를 만들려고합니다. 레일스 쪽의 User 모델은 모델에 has_secure_password, password_digest 필드를 가지고 있습니다.AngularJS Rails 사용자가 개발자없이 등록

문제 : (1) 레일스가 앵글 클라이언트에게 "암호가 누락되었습니다"라는 오류를 보냅니다. (2) 비밀번호와 확인은 Angular가 전송하지 않는 것 같습니다. (3) 비밀번호 다이제스트가 설정되지 않고 "nil"로 유지됩니다.

(1) 레일스는 앵글 클라이언트에게 "비밀번호가 누락되었습니다"라는 오류 메시지를 보냅니다.

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]"], ["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> 

편집 : 레일 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 

+0

이 레일 컨트롤러를 게시를 – user2936314

답변

1

나는 당신의 레일 컨트롤러는 암호 & 암호 확인이 때문에 허용하지 않도록 모델에 전달되도록 허용하지 않습니다 추측 것 강력한 매개 변수에서. 그것 또는 당신은 게시되는 JSON 사용자 객체에 포함시키지 않을 것입니다. (PARAMS는 [: 사용자]) 당신이 가장 가능성이 User.create을하고 있기 때문에, 당신은 당신이 모든 PARAMS 당신이 기대하고 허용했는지 확인해야합니다!

permitted_params = params[:user].permit(:username, :first_name, :last_name, :email, :password, :password_confirmation) 
User.create!(permitted_params) 
+0

나는 암호 및 허용하고 암호 확인은 강력한 매개 변수를 통해 모델로 전달됩니다. 내 컨트롤러가 게시됩니다. 당신의 도움을 주셔서 감사합니다. cc : @ user2936314 – user4109527

+0

브라우저의 웹 속성에서 네트워크 데이터가 생성 작업에 게시 된 것을 보여주는 것은 무엇입니까? 분명히 어떻게 든 비밀 번호 & 패스워드 확인은 당신의 문제인 params [: user] 해시 안에서 끝나지 않습니다 ... – patrick

+0

다음은 네트워크 데이터에서 볼 수 있습니다. 페이로드 요청 : { "username": "TheCatFelix", "first_name ":"TheCat ","last_name ":"Felix ","email ":"[email protected] ","password ":"password53 ","password_confirmation ":"password53 "} – user4109527

관련 문제