Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS + Railsでネストしたリソースを取得する
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def show
@project = Project.find(params[:id])
end
end
<div class="col-md-12">
<h2><%= @project.title %></h2>
<div ng-app="tasksApp" ui-view="" ng-init="projectId = <%= @project.id %>">
</div>
</div>
'use strict';
angular.module('tasksApp')
.controller('TasksController', ['$scope', 'Task', function($scope, Task){
$scope.currentTasks = Task.currentTasks({project_id: $scope.projectId});
$scope.iceboxTasks = Task.iceboxTasks({project_id: $scope.projectId});
}])
;
'use strict';
angular.module('tasksApp')
.provider('Task', function () {
this.$get = ['$resource', function ($resource){
var Task = $resource('/api/projects/:project_id/tasks/:id.json', { id: '@id', project_id: '@project_id' }, {
currentTasks: { method: 'GET', isArray: true, params: { task_type: 'current' }},
iceboxTasks: { method: 'GET', isArray: true, params: { task_type: 'icebox' }},
update: { method: 'PUT' }
}
);
Task.prototype = {
};
return Task;
}];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.