I have an angularjs application built into the rails application. I use the RESTful methods provided by the rails for a resource called Task. Here are the routes I'm dealing with:
GET /api/v1/tasks.json POST /api/v1/tasks.json GET /api/v1/tasks/:id.json PUT /api/v1/tasks/:id.json DELETE /api/v1/tasks/:id.json
I have an angularjs resource for a Task element, where the GET for all tasks works fine. (The code is in the coffee script)
App.factory "Task", ["$resource", "$cookies", ($resource, $cookies) -> $resource "/api/v1/tasks:slash:type.json?api_token=:api_token", api_token: $cookies.api_token type: '@id' , all: method: "GET" isArray: true create: method: "POST" update: method: "PUT" params: slash: "/" remove: method: "DELETE" ]
As you can see here, I am trying to insert a slash for the PUT method to get the format /api/v1/tasks/:id.json
. Unfortunately, angular puts this as %2f
, not a slash. I am not particularly concerned about the quality of this code, since the added slash
parameter makes it less readable.
Here is my angularjs controller using this resource:
taskController = App.controller 'TasksController', ($rootScope, $scope, $http, $cookies, Task) -> $scope.message = $cookies.api_token $scope.tasks = Task.all() $scope.selectedTask = null $scope.editTask = (task) -> $scope.selectedTask = task $scope.editDescription = task.description $rootScope.date = new Date(task.dueDate) console.log($rootScope.dt) $scope.taskModal = true $scope.saveTask = -> $scope.taskModal = false $scope.selectedTask.$update() # Task.update({taskID: $scope.selectedTask.id, task: $scope.selectedTask}) console.log 'Implement saving...' $scope.tasks = Task.all() $scope.cancelTask = -> $scope.taskModal = false $scope.taskModalOptions = { dialogFade: true } taskController.$inject = ['$rootScope', '$scope', '$http', '$cookies', 'Task']
Basically, my question is, does anyone have an example of how to reproduce traditional rails / RESTful URL formats with angularjs resource? I looked through a few StackOverflow questions and didn't seem to find a good answer. Any help is appreciated.