Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用Angular将嵌套资源发布到Rails后端_Angularjs_Ruby On Rails 4_Nested Forms_Nested Attributes - Fatal编程技术网

Angularjs 使用Angular将嵌套资源发布到Rails后端

Angularjs 使用Angular将嵌套资源发布到Rails后端,angularjs,ruby-on-rails-4,nested-forms,nested-attributes,Angularjs,Ruby On Rails 4,Nested Forms,Nested Attributes,我是一个Angular新手,正在尝试使用Angular将嵌套资源发布到我的Rails后端。我使用两种模型:帖子和评论。一篇帖子有很多评论。我通过我的PostsController中的“includes”来呈现我的评论和帖子 我可以通过我的浏览器用angular创建新帖子;然而,我一辈子都不知道如何创建新的评论。在尝试输入注释时,我在js控制台中遇到以下错误: POST http://localhost:3000/posts/1/comments 400 (Bad Request) angular

我是一个Angular新手,正在尝试使用Angular将嵌套资源发布到我的Rails后端。我使用两种模型:帖子和评论。一篇帖子有很多评论。我通过我的PostsController中的“includes”来呈现我的评论和帖子

我可以通过我的浏览器用angular创建新帖子;然而,我一辈子都不知道如何创建新的评论。在尝试输入注释时,我在js控制台中遇到以下错误:

POST http://localhost:3000/posts/1/comments 400 (Bad Request) angular.js?body=1:9313
我在rails控制台中遇到以下错误:

Started POST "/posts/1/comments" for 127.0.0.1 at 2013-09-02 18:41:24 -0700
Processing by CommentsController#create as JSON
  Parameters: {"post_id"=>"1"}
Completed 400 Bad Request in 1ms

ActionController::ParameterMissing (param not found: comment):
  app/controllers/comments_controller.rb:34:in `comment_params'
  app/controllers/comments_controller.rb:12:in `create' 
我尝试了几种不同的方法,并试图复制本文()中的逻辑,但没有效果

这是我的工厂:

factoriesModule.factory "Post", ["$resource", ($resource) ->
  $resource("/posts/:id", {id: "@id"},{
  update: {method: "PUT"}
})
]

factoriesModule.factory "Comment", ["$resource", ($resource) ->
  $resource("/posts/:post_id/comments/:id", {id: "@id", post_id: "@post_id"},
  update: {method: "PUT"}
)
]
这是我的控制器:

@PostingCtrl = ["$scope", "Post", ($scope, Post) ->
$scope.posts = Post.query()

$scope.addPost = ->
post = Post.save($scope.newPost)
$scope.posts.push(post)
$scope.newPost = {}
]

@CommentingCtrl = ["$scope", "Comment", "$http", ($scope, Comment, $http) ->
 $scope.comments = Comment.query({post_id: @post_id, id: @id})

$scope.addComment = (post_id, data) ->
  console.log $scope.newComment.text
$http.post("/posts/" + post_id + "/comments", data).success alert 'hi' 
]

我将$http服务用于addComment,因为这是我发现的唯一一种为我的评论提供post_id(使用我的视图逻辑)的方法。为冗长的帖子道歉,并感谢您的帮助

可能我在这里没有看到所有的代码/html,但根据控制器错误,您似乎没有遇到param[“comment”]。从你的addComment方法中,我看不出是谁在调用它。我假设它是从一个html页面调用的,即ng submit=“addComment()”。如果为true,则需要确保字段的前缀为“comment”。否则,服务器上的参数将为“flat”。这个问题与AngularJS无关。