0

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
1
  • Well, after looking a little longer I found that adding .json in the resource url parameter fixes the problem: $resource("/users/:id.json", {id: "@id"}, {update: {method: "PUT"}}) Commented Nov 18, 2013 at 0:54

2 Answers 2

3

Add isArray:false to resource declaration, for example:

'query': {method: 'GET', isArray: false }
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.