How do I save an Angular form to my ruby on rails backend?

后端 未结 3 2064
清酒与你
清酒与你 2021-01-05 18:46

I\'m new to Angular. I\'ve tried everything I know how and Google searches have surprisingly few tutorials on this particular question. Here\'s the last code I tried:

<
3条回答
  •  抹茶落季
    2021-01-05 19:11

    I've worked a lot with Angular and Rails, and I highly recommend using AngularJS Rails Resource. It makes working with a Rails backend just that much easier.

    https://github.com/FineLinePrototyping/angularjs-rails-resource

    You will need to specify this module in your app's dependencies and then you'll need to change your factory to look like this:

    app.factory('Article', function(railsResourceFactory) {
      return railsResourceFactory({url: '/articles', name: 'article');
    });
    

    Basically, based on the error that you are getting, what is happening is that your resource is not creating the correct article parameter. AngularJS Rails Resource does that for you, and it also takes care of other Rails-specific behavior.

    Additionally, $scope.newPost should not be Article.save(). You should initialize it with a new resource new Article() instead.

提交回复
热议问题