Skip to content

Commit 476f5a4

Browse files
author
Jaco Pretorius
committed
Making routes RESTful. Adding ability to delete, still no refresh after delete though
1 parent 3d0c2ba commit 476f5a4

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

app/assets/javascripts/controllers/todo_controller.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function TodoController($scope, $http, $location) {
22

3-
$http.get('/todo/index.json').success(function(data) {
3+
$http.get('/todo.json').success(function(data) {
44
$scope.items = data;
55
});
66

@@ -10,12 +10,19 @@ function TodoController($scope, $http, $location) {
1010

1111
$scope.addTodo = function() {
1212
var description = $scope.newTodo.description;
13-
$http.post('/todo/add.json', { description: description }).success(function() {
13+
$http.post('/todo.json', { description: description }).success(function() {
1414
$scope.newTodo.description = '';
1515
$location.path("list");
1616
});
1717
}
1818

19+
$scope.deleteTodo = function(todo) {
20+
var id = todo._id;
21+
$http.delete('/todo/' + id).success(function() {
22+
$location.path("list");
23+
});
24+
}
25+
1926
$scope.backToList = function() {
2027
$location.path("list");
2128
}

app/assets/javascripts/views/list.jst.ejs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<ul>
22
<li data-ng-repeat="item in items">
3-
{{item.description}}
3+
<div><label>Description: {{item.description}}</label></div>
4+
<div><button data-ng-click="deleteTodo(item)">Delete</button></div>
45
</li>
56
</ul>
67
<div>

app/controllers/todo_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def index
33
render :json => Todo.all
44
end
55

6-
def add
6+
def create
77
if Todo.create(params[:todo])
88
head :ok
99
else
@@ -14,6 +14,12 @@ def add
1414
def update
1515
end
1616

17-
def delete
17+
def destroy
18+
puts params
19+
Todo.find(params[:id]).delete
20+
head :ok
21+
rescue Exception => e
22+
puts e
23+
head :bad_request
1824
end
1925
end

config/routes.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
AngularRails::Application.routes.draw do
2-
get "todo/index"
3-
post "todo/add"
4-
put "todo/update"
5-
delete "todo/delete"
2+
resources :todo, only: [:index, :create, :update, :destroy]
63

74
root :to => 'todo#index'
85
end

0 commit comments

Comments
 (0)