gpt4 book ai didi

ruby-on-rails - Angular 和 Rails 浅层资源

转载 作者:行者123 更新时间:2023-12-04 03:54:30 24 4
gpt4 key购买 nike

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 之外的另一个模块?或者我应该只构建两个服务:评论和评论?

最佳答案

我实际上是从 github 上查看了 angular 资源的源代码,发现当前 master 允许操作指定自己的 url 参数。我在我的问题中写的示例完全可以使用 Angular 1.1.4+(尚未发布)。

升级到当前的 Angular 大师是解决方案。

关于ruby-on-rails - Angular 和 Rails 浅层资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15679132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com