angularjs - Using Angular and Rails: Bcrypt throwing an error when trying to create user -
i working on app use angular , rails. trying create new user, keep getting error:
bcrypt::errors::invalidhash (invalid hash): app/models/user.rb:14:in `new' app/models/user.rb:14:in `password' app/controllers/users_controller.rb:8:in `create'
it seems angular creating password, not getting passed rails.
parameters: {"first_name"=>"john", "last_name"=>"smith", "email"=>"john.smith@gmail.com", "username"=>"johnsmith", "password"=>"[filtered]", "user"=>{"first_name"=>"john", "last_name"=>"smith", "username"=>"johnsmith", "email"=>"john.smith@gmail.com"}}
here frontend code looks like:
app.js
$scope.signup = function() { var newuser = {first_name: $scope.firstname, last_name: $scope.lastname, email: $scope.email, username: $scope.username, password: $scope.password} $http.post('http://localhost:3000/users', newuser) .success(function(data) { console.log(data); if (data.errors) { console.log(data.errors); } else { $location.path("/sign_in"); } }) };
signup.html
<div class="form-group has-success signup-box"> <form name="signupform" novalidate> <input type="text" ng-required="true" class="form-control" placeholder="first name" ng-model="firstname"/> <input type="text" ng-required="true" class="form-control" placeholder="last name" ng-model="lastname"/> <input type="text" ng-required="true" class="form-control" placeholder="email" ng-model="email"/> <input type="text" ng-required="true" class="form-control" placeholder="username" ng-model="username"/> <input type="text" ng-required="true" class="form-control" placeholder="password" ng-model="password"/> <input type="submit" ng-disabled="signupform.$invalid" class="form-control btn btn-success" value="sign up" ng-click="signup()" /> </form> <a href="/#/sign_in">sign in</a> </div>
and backend code:
users_controller.rb
class userscontroller < applicationcontroller def create user = user.new(user_params) if user.save render json: "you create account. please log in information." else render json: user end end private def user_params params.require(:user).permit(:first_name, :last_name, :username, :email, :password) end end
user.rb
require 'bcrypt' class user < activerecord::base include bcrypt *** validates :first_name, :last_name, :username, :email, :password, presence: true *** def password @password ||= password.new(password_hash) end def password=(new_password) @password = password.create(new_password) self.password_hash = @password end end
any appreciated!
i able rid of error. apparently, rails did not format of data being passed in. in app.js file on line 2 (see question) replaced:
{first_name: $scope.firstname, last_name: $scope.lastname, email: $scope.email, username: $scope.username, password: $scope.password}'
with
{user: {first_name: $scope.firstname, last_name: $scope.lastname, email: $scope.email, username: $scope.username, password: $scope.password}}