ActionController :: RoutingError(没有路由匹配[POST]" / forums / comments")

时间:2014-08-09 09:09:32

标签: ruby-on-rails angularjs ruby-on-rails-4

我在新的rails项目中使用inherited_resources 1.5.0 gemangularjs。我有2个型号:forumcomment。我使用angularjs创建所有表单并从用户获取数据并发布到rails服务器以创建新方法。对于论坛,没关系,但是当我想向论坛添加评论(has_many :comments, :dependent => :destroy)时,我在server log中得到以下错误:

ActionController::RoutingError (No route matches [POST] "/forums/comments")

当我运行rake routes时,我会看到以下路由:

    forum_comments GET    /forums/:forum_id/comments(.:format)          comments#index {:format=>:json}
                   POST   /forums/:forum_id/comments(.:format)          comments#create {:format=>:json}
 new_forum_comment GET    /forums/:forum_id/comments/new(.:format)      comments#new {:format=>:json}
edit_forum_comment GET    /forums/:forum_id/comments/:id/edit(.:format) comments#edit {:format=>:json}
     forum_comment GET    /forums/:forum_id/comments/:id(.:format)      comments#show {:format=>:json}
                   PATCH  /forums/:forum_id/comments/:id(.:format)      comments#update {:format=>:json}
                   PUT    /forums/:forum_id/comments/:id(.:format)      comments#update {:format=>:json}
                   DELETE /forums/:forum_id/comments/:id(.:format)      comments#destroy {:format=>:json}
            forums GET    /forums(.:format)                             forums#index {:format=>:json}
                   POST   /forums(.:format)                             forums#create {:format=>:json}
         new_forum GET    /forums/new(.:format)                         forums#new {:format=>:json}
        edit_forum GET    /forums/:id/edit(.:format)                    forums#edit {:format=>:json}
             forum GET    /forums/:id(.:format)                         forums#show {:format=>:json}
                   PATCH  /forums/:id(.:format)                         forums#update {:format=>:json}
                   PUT    /forums/:id(.:format)                         forums#update {:format=>:json}
                   DELETE /forums/:id(.:format)                         forums#destroy {:format=>:json}

forums_controller.rb

class ForumsController < InheritedResources::Base
  respond_to :json

  def forum_params
    params.require(:forum).permit(:name)
  end
end

forum.rb

class Forum < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
end

comments_controller

class CommentsController < InheritedResources::Base
  belongs_to :forum
  respond_to :json

  def comment_params
    params.require(:comment).permit(:forum_id, :name, :body)
  end
end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :forum
end

我在angularjs中有以下代码:

'use strict';

var app = angular.module('app');
app.controller('CommentsController', ['$scope', 'Comment', '$routeParams', function($scope, Comment, $routeParams) {
    //Grab all the comments from the server
    $scope.comments = Comment.query({forum_id: $routeParams.id});

    //Define a 'save' method which will be called from the view.
    $scope.save = function() {
        //Create the comment object to be sent to the server
        var obj = new Comment({name: $scope.name, body: $scope.body, forum_id: $routeParams.id});

        //Attempt a save to the back-end
        obj.$save(function(response) {

            //If we're successful then add the response (the object as the server sees it)
            // to our collection of comments
            $scope.comments.unshift(response);

            //Empty the name & body
            $scope.name = $scope.body = ""

        }, function(response) {

            //If there's a failure set the 'errors' scope variable so it'll be reflected in the view.
            $scope.errors = response.data.errors;
        });
    }
}]);

当我发布数据时,我在chrome控制台中也出现了bwlow错误:

POST http://localhost:3000/forums/comments?forum_id=7 404 (Not Found) angular.min.js?body=1:81
(anonymous function) angular.min.js?body=1:81
t angular.min.js?body=1:76
f angular.min.js?body=1:74
I angular.min.js?body=1:102
I angular.min.js?body=1:102
(anonymous function) angular.min.js?body=1:103
h.$eval angular.min.js?body=1:114
h.$digest angular.min.js?body=1:111
h.$apply angular.min.js?body=1:115
(anonymous function) angular.min.js?body=1:203
jQuery.event.dispatch jquery.js?body=1:4642
elemData.handle jquery.js?body=1:4310

如何解决此问题并将路线设置为/forums/comments

1 个答案:

答案 0 :(得分:1)

我将评论发布到错误的网址。我的angularjs代码中有一个model.js,我在这个js文件中设置了url。我更正了下面的代码:

angular/model.js

'use strict';    
    var app = angular.module('app');
    app.factory('Comment', ['$resource', function($resource) {
        return $resource('/forums/:forum_id/comments/:id', {forum_id: '@forum_id', id: '@id'});
    }]);

现在angularjs POST数据到/forums/:forum_id/comments(.:format) comments#create {:format=>:json}和rails允许将数据添加到数据库。