Rails 为我依赖的嵌套资源提供了一个 :shallow 选项,否则我将拥有 4 或 5 个嵌套资源,这将非常糟糕。
浅层资源如下所示:
resources :posts do
resources :comments, :only => [:index, :create]
end
resources :comments, :only => [:show, :update, :destroy]
我遇到的问题是在尝试使用 Angular 资源来映射 Rails 资源时,因为 ngResource 只允许定义单个 URI,所以要么/posts/:post_id/comments/:id
要么/comments/:id
.
我想要的是 ngResource 允许方法覆盖 URI 和插值。例如:
app.factory('Comment', ['ngResource', function ($resource) {
$resource('/comments/:id', { 'id': '@id', post_id: '@post_id' },
index: { method: 'GET', url: '/posts/:post_id/comments', isArray: true },
create: { method: 'POST', url: '/posts/:post_id/comments' },
show: { method: 'GET' },
update: { method: 'PUT' },
destroy: { method: 'DELETE' }
);
}]);
这可能吗,也许是 ngResource 之外的另一个模块?或者我应该只构建两个服务:评论和评论?