Reputation: 1021
I use inherited_resources 1.5.0 gem
and angularjs
in my new rails project. I have 2 model: forum
and comment
. I create all form with angularjs
and get data from user and post to rails server to create new method. For forum, it's ok, but when I want add comment to forums (has_many :comments, :dependent => :destroy
), I get below error in server log
:
ActionController::RoutingError (No route matches [POST] "/forums/comments")
when I run rake routes
I see below routing:
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
and I have below code in 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;
});
}
}]);
And when I post data, I get bwlow error in chrome console too:
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
How can I fix this problem and set route to /forums/comments
?
Upvotes: 1
Views: 854
Reputation: 1021
I post comments to wrong url. I have a model.js
in my angularjs code and I set the url in this js file. I correction the code like below:
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'});
}]);
Now angularjs POST data to /forums/:forum_id/comments(.:format) comments#create {:format=>:json}
and rails permit to add data to database.
Upvotes: 1