Rails 4.2 - 为 belongs_to 模型创建动作(Thinkster.io Angular + Rails 教程)

Rails 4.2 - Creating an action for a belongs_to model (Thinkster.io Angular + Rails Tutorial)

我从这里 https://thinkster.io/angular-rails/ 开始学习 AngularJS + Rails 教程,并且 运行 在 "Finishing Off Comments" 部分的墙内接近尾声(就在它说 "To enable adding comments, we can use the same technique we used for adding new posts" 之后)。具体来说,当我点击 /posts/{id}/comments.json 端点时,服务器抛出 500。

我得到的错误是 undefined local variable or method `post' for #<CommentsController:0x5f72b38>

post.rb:

class Post < ActiveRecord::Base
    has_many :comments

    def as_json(options = {})
        # Make all JSON representations of posts include the comments
        super(options.merge(include: :comments))
    end
end

comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :post
end

postsCtrl.js:

angular.module('flapperNews')
.controller('PostsCtrl', [
    '$scope',
    'posts',
    'post',
    function($scope, posts, post) {
        $scope.post = post;

        $scope.addComment = function(){
          if($scope.body === '') { return; }
          posts.addComment(post.id, {
            body: $scope.body,
            author: 'user'
          }).success(function(comment) {
            $scope.post.comments.push(comment)
          });

          $scope.body = '';
        };
    }]);

posts.js:

angular.module('flapperNews')
.factory('posts', [
    '$http',
    function($http) {
        // Service Body
        var o = {
            posts: []
        };

        o.getAll = function() {
            return $http.get('/posts.json').success(function(data) {
                angular.copy(data, o.posts)
            });
        };

        o.create = function(post) {
            return $http.post('/posts.json', post).success(function(data) {
                o.posts.push(data);
            });
        };

        o.upvote = function(post) {
            return $http.put('/posts/' + post.id + '/upvote.json')
                .success(function(data) {
                    post.upvotes += 1;
                });
        }

        o.get = function(id) {
            return $http.get('/posts/' + id + '.json').then(function(res) {
                return res.data;
            });
        };

        o.addComment = function(id, comment) {
            return $http.post('/posts/' + id + '/comments.json', comment);
        }

        return o;
}]);

最后 comments_controller.rb:

class CommentsController < ApplicationController
    def create
        comment = post.comments.create(comment_params)
        respond_with post, comment
    end

    def upvote
        comment = post.comments.find(params[:id])
        comment.increment!(:upvotes)

        respond_with post, comment
    end

    private
    def comment_params
        params.require(:comment).permit(:body)
    end
end

我知道它在抱怨创建操作中对 post 的引用,但我不知道为什么 Rails 不只是将其识别为 post评论属于。我是 Rails 的新手,但我看不出我所做的与教程有什么不同。非常感谢!

天哪,我现在觉得自己很蠢。我将以下行添加到创建操作的顶部,它现在可以工作了:post = Post.find(params[:post_id]) 希望这能帮助其他任何被卡住的人!教程好像少了这部分。