我目前正在尝试在带有角度前端的rails中构建一个小型Todo列表应用程序。目前,我能够创建一个Todo对象并将其保存到数据库,但我无法在页面刷新时返回所有Todo对象。当我加载页面时,Todo计数为零,并且在控制台中看起来我正在返回一个空数组。我相信问题与控制器有关,我试图从索引操作查询所有待办事项。这是我的rails控制器:
class TodosController < ApplicationController
respond_to :json
def index
respond_with Todo.all
end
def show
respond_with Todo.find(params[:id])
end
def create
respond_with Todo.create(params[:todo])
end
def update
respond_with Todo.update(parms[:id], params[:todo])
end
def destroy
respond_with Todo.destroy(params[:id])
end
end
这是我的看法。我有重复:
<div ng-controller="TodoController">
<h2 class="index_header">Total todos: {{getTotalTodos()}}</h2>
<ul>
<li ng-repeat="todo in todos | orderBy:'-importance'">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}} todos">{{todo.title}} - {{todo.importance}}</span>
</li>
</ul>
<form class="form-inline">
<input type="text" ng-model="newTodo.title" placeholder="item" class="placeholder">
<input type="text" ng-model='newTodo.description' placeholder="description" class="placeholder">
<input type="number" ng-model="newTodo.importance" placeholder="importance" class="placeholder">
<button class="styled-button-2" ng-click="addTodo()">Add Item</button><br>
<span class="binding">{{newTodo.title}}</span><br>
{{newTodo.description}}<br>
{{newTodo.importance}}
</form>
<button class="styled-button-5" ng-click="clearCompleted()">Clear Completed Items</button>
</div>
这是我的js文件,其中包含我的TodoController:
angular.module("Todo", ["ngResource"])
function TodoController($scope, $filter, $resource) {
Todo = $resource("/todos", {id: "@id"}, {update: {method: 'PUT'}})
$scope.todos = Todo.query()
$scope.getTotalTodos = function () {
return $scope.todos.length;
};
$scope.clearCompleted = function () {
Todo.save({done:true})
$scope.todos = $filter("filter")($scope.todos, {done:false});
};
$scope.addTodo = function () {
entry = Todo.save({title:$scope.newTodo.title, importance:$scope.newTodo.importance, description:$scope.newTodo.description, done:false})
$scope.todos.push(entry);
$scope.newTodo.title = '';
$scope.newTodo.importance = '';
$scope.newTodo.description = '';
};
}
同样,我能够创建一个保存到数据库的对象,并在创建后显示在列表中并将计数移动到一个。刷新页面后,不会显示新创建的对象,计数将返回零。我在google论坛上问了这个问题,有人告诉我这是服务器问题。