2

I created a basic API with Ruby on Rails. Whenever I try to send data from a form in AngularJS, I get this message in the Rails Server:

Parameters: {"{\"content\":\"message\"}"=>nil}

So, it's creating null records in the DB.

This is the controller in AngularJS to send the data:

app.controller('postController', function($scope, $http) {
  // create a blank object to handle form data.
    $scope.message = {};
  // calling submit function.
    $scope.submitForm = function() {

    $http({
      method  : 'POST',
      url     : 'http://localhost:3000/api/v1/messages',
      data    :  $scope.message, //forms user object
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
      .success(function(data) { ... }
      });
    };
});
1
  • have you verified the contents of $scope.message before you POST? What if you remove the content type? Commented Feb 17, 2016 at 21:13

2 Answers 2

3

You need to serialize the data when you send as x-www-form-urlencoded

Example copied from docs

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

Or use the defaults of $http which sends JSON in request body as application/json:

$http.post(myurl, data).then(...;
Sign up to request clarification or add additional context in comments.

3 Comments

(Part 1) Thank you, now is the Rails Server is throwing me a different error: ArgumentError (When assigning attributes, you must pass a hash as an argument.)
Create Method in rails is: def create respond_with Message.create(params[:content]) end
Ok, don't worry: I fixed it with this: data : {'message': $scope.message} Thank you a lot
2

change this line:

headers : {'Content-Type': 'application/x-www-form-urlencoded'} 

to:

headers : {'Content-Type': 'application/json'} 

Also encode to Json if it isnt like this:

  data    :  angular.toJson($scope.message), //forms user object

This way you will send the correct JSON formatted data to API, make sure your API accepts Json encoded data just in case.

7 Comments

no need to set 'application/json' ... that is the default and don't use toJson that is already done internally. Can see it already in OP's request
hey, you are right but thought it would be better to have the default written instead of what i thought wouldnt work: x-www-form-urlencoded
except then OP would think it was needed every time and others who read this later with similar problems might think so also
you saw the best solution for sure, this is how I would have started to solve the problem. I just see it weird sending this content type application/x-www-form-urlencoded for json data
(Part 1) This solution worked too, but now Rails is throwing me a different message: ArgumentError (When assigning attributes, you must pass a hash as an argument.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.