1

I created a simple API with Ruby on Rails,and I'm trying to send data from a form with AngularJS. The thing is that the App is sending the post method and the backend is creating the new records, so far it's working, but it creates it with Null values.

This is the form:

<div ng-controller="postController">
    <form name="messageForm" ng-submit="submitForm()">
        <div class="form-group">
            <label>Name</label>
            <input type="text" name="content" class="form-control" ng-model="message.content">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>

This is the Angular Controller:

app.controller('postController', function($scope, $http) {
  // create a blank object to handle form data.
    $scope.message = {};
  // calling our 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) {
        if (data.errors) {
          // Showing errors.
          $scope.errorContent = data.errors.errorContent;
        } else {
          $scope.message = data.message;
        }
      });
    };
});

And in the RoR's API Controller, this is the Create method:

def create 
    respond_with Message.create(params[:message])
end

The only rows are:

  • ID which is generated automatically.
  • And 'content'

1 Answer 1

2

You must assign a key to your message object in data parameter, like this:

$http({
  method  : 'POST',
  url     : 'http://localhost:3000/api/v1/messages',
  data    : { data: $scope.message },
  headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
 })
 .sucess(function(data){ ... });

And then, in your controller:

def create 
  respond_with Message.create(params[:data])
end

Hope this help :)

Sign up to request clarification or add additional context in comments.

3 Comments

(Part 1) Thank you for replaying! This helped, but now I'm getting this in the console: "XMLHttpRequest cannot load localhost:3000/api/v1/messages. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '127.0.0.1:59076' is therefore not allowed access. The response had HTTP status code 500."
(Part 2) But I have this method in my Application Controller: before_filter :add_allow_credentials_headers def add_allow_credentials_headers response.headers['Access-Control-Allow-Origin'] = request.headers['Origin'] || '*' response.headers['Access-Control-Allow-Credentials'] = 'true' end
@GabrielAlejandro If you still getting that error, open it in a new question to get more attention!

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.