Skip to content

Instantly share code, notes, and snippets.

@mahm
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahm/7deb7549a8f50ec5a073 to your computer and use it in GitHub Desktop.
Save mahm/7deb7549a8f50ec5a073 to your computer and use it in GitHub Desktop.
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