Cannot save with AngularJS to Rails Backend

时间:2015-07-31 20:05:09

标签: javascript ruby-on-rails json angularjs

We are creating a "twitter clone" using Angular JS and Rails postgresql backend. We have a form to submit new content for a tweet. We have been using AngularJS Resource to grab jSON objects. This is the form below:

<section id="tweet-box" ng-controller="TweetController" action="/tweets" method="post">
    <p id="tweet-box-title">Compose New Tweet</p>
    <form id="tweet-form" ng-submit="addTweet()">
      <textarea id="new-tweet" cols="30" rows="5" maxlength="140" name="tweet" ng-model="content"></textarea>
      <button type="submit" value="Tweet">Add Tweet</button>
    </form>
</section> 

Here is our AngularJS Code. tweetApp is our entire App; the factory grabs our 50 most recent Tweets as jSON. The addTweet() function is what we're trying to have save our data to our database.

var tweetApp = angular.module("tweetApp", ['ngResource']);

tweetApp.factory("RecentTweets", function($resource)
{
  return $resource("/tweets/recent");
});

tweetApp.controller("TweetController", function($scope, RecentTweets)
{
  $scope.tweets = RecentTweets.query(function(data)
  {
    data;

  });
  $scope.addTweet = function(){
    if ($scope.content){
    $scope.tweet = new Tweet();
    $scope.tweet.content = $scope.content;

    // Tweet.save($scope.tweet);
    $scope.tweet.$save(function(msg, header){
    });
    }
    $scope.content = null;
  }
});

At the very end, $save vs .save both come up as not a function in our console. We have tried Tweet.save as well as $scope.content.$save as well as other iterations. Please advise. Thanks!

0 个答案:

没有答案