Ruby on rails 角度+;轨道&x2B;MongoDB(mongoid)删除函数赢得';行不通
我有一个带有rails后端的Angular应用程序,使用mongoid gem作为数据库。我正在尝试创建一个删除函数,但无法使其运行。 我明白了 我好像拿不到身份证。我以为我是在工厂里做的。否则,我不知道如何得到它 我的代码: 角度:Ruby on rails 角度+;轨道&x2B;MongoDB(mongoid)删除函数赢得';行不通,ruby-on-rails,angularjs,mongodb,mongoid,Ruby On Rails,Angularjs,Mongodb,Mongoid,我有一个带有rails后端的Angular应用程序,使用mongoid gem作为数据库。我正在尝试创建一个删除函数,但无法使其运行。 我明白了 我好像拿不到身份证。我以为我是在工厂里做的。否则,我不知道如何得到它 我的代码: 角度: var myApp = angular.module('tagsapp', ['ngRoute', 'ngResource']); myApp.factory("Tag", function($resource) { return $resource("/a
var myApp = angular.module('tagsapp', ['ngRoute', 'ngResource']);
myApp.factory("Tag", function($resource) {
return $resource("/api/tags/:id", { _id: "@id"},
{
'create': { method: 'POST' },
'index': { method: 'GET', isArray: true },
'show': { method: 'GET', isArray: false },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }
}
);
})
// Controllers
myApp.controller("TagListCtrl", ['$scope', '$resource', 'Tag', '$location',
function($scope, $resource, Tag, $location) {
$scope.tags = Tag.query();
$scope.saveNewTag = function() {
Tag.create({ tag: $scope.newTag }, function(response){
$scope.tags.push(response);
$scope.newTag = null;
});
}
$scope.deleteTag = function(tagId) {
Tag.destroy({ _id: tagId }, function(response){
var index = $scope.tags.indexOf(tagId);
$scope.tags.splice(index, 1)
$location.path('/')
})
};
}]);
和我的rails控制器:
class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy]
respond_to :json
def index
@tags = Tag.all
end
def show
@tag = Tag.find(params[:id])
end
def new
@tag = Tag.new
end
def edit
@tag = Tag.find(params[:id])
end
def create
# @tag = Tag.new(tag_params)
tag = Tag.create!(tag_params)
render json: tag, status: 201
end
def update
tag.update_attributes(tag_params)
render json: tag, status: 201
end
def destroy
@tag.destroy
respond_with @tag
end
private
def set_tag
@tag = Tag.find(params[:id])
end
def tag_params
params.require(:tag).permit(:name, :color, :order)
end
end
我怀疑我的路线错了。到目前为止,在rails
index.html.erb
中添加一个新标记是有效的。我没有为angular添加任何要路由到的模板。您的路由定义是什么样子的?@miensol为您编辑了我的帖子
class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy]
respond_to :json
def index
@tags = Tag.all
end
def show
@tag = Tag.find(params[:id])
end
def new
@tag = Tag.new
end
def edit
@tag = Tag.find(params[:id])
end
def create
# @tag = Tag.new(tag_params)
tag = Tag.create!(tag_params)
render json: tag, status: 201
end
def update
tag.update_attributes(tag_params)
render json: tag, status: 201
end
def destroy
@tag.destroy
respond_with @tag
end
private
def set_tag
@tag = Tag.find(params[:id])
end
def tag_params
params.require(:tag).permit(:name, :color, :order)
end
end
<div ng-controller="TagListCtrl" ng-style="{color: myColor}">
<table>
Name: <input ng-model="newTag.name"> <br>
Color: <input ng-model="newTag.color"> <br>
Order: <input ng-model="newTag.order"> <br>
<button ng-click="saveNewTag()">Save</button>
</div>
<tr ng-show="tags.length" ng-repeat="tag in tags">
<td>{{tag.name}}</td>
<td>{{tag.color}}</td>
<td>{{tag.order}}</td>
<td>
<a href="" ng-click="deleteTag(tag.id)">Remove</a>
</td>
</tr>
</table>
</div>
Rails.application.routes.draw do
scope "api", defaults: {format: :json} do
resources :tags
end
root to: "tags#index", anchor: false
end