错误的资源配置,而使用$ resource.query()函数AngularJS / Rails的 [英] Error in resource configuration while using $resource.query() function with AngularJS/Rails

查看:130
本文介绍了错误的资源配置,而使用$ resource.query()函数AngularJS / Rails的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度和我试图将它添加到我的Rails项目之一。我使用的角轨宝石。我想列出使用$ resource.query(用户),我收到以下错误:


  

错误:[$资源:badcfg]错误的资源配置。期望的回应包含一个数组,但得到的对象


当我检查本地主机:3000 / users.json它显示了用户对象的数组,所以反应应该是正确的格式。下面是我有:

在应用程序/资产/ JavaScript的/ users.js.coffee:

 应用程序= angular.module(志愿者,[ngResource])app.factory用户,[$资源,($资源) -  GT;
  $资源(/用户/:ID,{ID:@id},{更新:{方法:PUT}})
]@UserCtrl =$范围,用户,($范围,用户) - GT;
  $ scope.users = User.query()
]

在应用程序/视图/用户/ index.html的:

 < D​​IV NG控制器=UserCtrl>
  < UL>
    <李NG重复=用户用户>
      {{用户名}}
    < /李>
  < / UL>
< / DIV>

而在users_controller:

 类UsersController< ApplicationController中
    respond_to代码:JSON,:HTML    高清指数
        respond_with User.all
    结束


解决方案

  app.factory用户,[$资源,($资源) -  GT;
$资源(/用户/:id.json,{ID:@id},{查询:{方法:PUT}})
]

你有没有敲错查询,以更新?

如果你得到的是数组,你应该为我的家伙上面说的设置IsArray的= true,否则将其设置为false,一样的。

I'm new to angular and I'm trying to add it to one of my Rails projects. I'm using the angular-rails gem. I want to list users using $resource.query() and I'm getting the following error:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

When I check localhost:3000/users.json it's showing an array of user objects, so the response should be in the correct format. Here's what I have:

In app/assets/javascript/users.js.coffee:

app = angular.module("Volunteers", ["ngResource"])

app.factory "User", ["$resource", ($resource) ->
  $resource("/users/:id", {id: "@id"}, {update: {method: "PUT"}})
]

@UserCtrl = ["$scope", "User", ($scope, User) ->
  $scope.users = User.query()
]

In app/views/users/index.html:

<div ng-controller="UserCtrl">
  <ul>
    <li ng-repeat="user in users">
      {{user.name}}
    </li>
  </ul>
</div>

And in the users_controller:

class UsersController < ApplicationController
    respond_to :json, :html 

    def index
        respond_with User.all
    end

解决方案

app.factory "User", ["$resource", ($resource) ->
$resource("/users/:id.json", {id: "@id"}, {query: {method: "PUT"}})
]

Did you mistype 'query' to 'update'?

If what you get is array, you should set isArray=true, else set it false, same as the guy said above me.

这篇关于错误的资源配置,而使用$ resource.query()函数AngularJS / Rails的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆