dimanche 28 juin 2015

Angular POST request sending info in parameters that I did not specify and Rails throwing errors about it

So I am currently making a user create a new account on a Rails app using devise.

The registration is handled by Angular on the client side.

Here is the angular code for it:

This method is in a controller

$scope.create = (isValid) ->
            $scope.submitted = true
            if isValid
                UserService.create_user(user: {
                    name: $scope.name,
                    email: $scope.email,
                    password: $scope.password,
                    password_confirmation: $scope.password_confirmation
                }, submit: 'Sign up', (response)->
                    console.log(response)
                )
                console.log(isValid)
                #@userService.createUser(user, $scope)
            else

@Fit.service 'UserService', [
    '$resource', '$http', '$routeParams', 'Common',
    ($resource, $http, $routeParams, Common) ->
        $resource '/users/:id/:options.json', { id: '@id', options: '@options' },
            create_user: {
                method: 'POST'
            },
            set_avatar: {
                method: 'POST'
            },
            update_user: {
                method: 'PATCH'
            },
            find: {
                method: 'GET'
            },
            update_role: {
                method: 'PATCH'
            }

]

Now when I send the request on the way I get this in my server log:

Started POST "/users.json" for 127.0.0.1 at 2015-06-28 22:45:35 +0300
Value for params[:user][:password] was set to nil, because it was one of [], [null] or [null, null, ...]. Go to http://ift.tt/1vKj4i4 for more information.
Processing by Devise::RegistrationsController#create as JSON
  Parameters: {"user"=>{"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "submit"=>"Sign up", "registration"=>{"user"=>{"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "submit"=>"Sign up"}}
   (0.2ms)  BEGIN
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'foo@bar.com' LIMIT 1
   (0.2ms)  ROLLBACK
Completed 422 Unprocessable Entity in 5ms (Views: 0.2ms | ActiveRecord: 0.8ms)

Now there are two things wrong with this log. First of all in the parameters section I have a list of the same values that I put under user object also under the registration object. Now I have no idea where that came from.

As you can see in the picture, chrome debugger shows that no such params where sent. Only the user one. Yet rails logs show differently.

My request params

Now the second problem here is that for some reason the log is throwing errors about the params I sent and I also get this response:

{"errors":{"password":["can't be blank"],"password_confirmation":["doesn't match Password"]}}

What could be causing these issues? I checked up what is being sent if I use .erb user creation with a new Devise project and there it also sent all the params under the user object.

Aucun commentaire:

Enregistrer un commentaire