Domanda

here's the relevant snippet of code:

$scope.submit = function() {
  console.log(this);
  weekly_habits = $resource('/api/users/:user_id/weekly_habits/:id/', {user_id: '@user'});
  entry = weekly_habits.save({name: $scope.newAccomp, count: 0});
  $scope.accomplishments.unshift(entry);
  $scope.newAccomp = '';
}

my error is that no route matches /api/users/weekly_habits... My project is built in rails and I don't understand what this line means {user_id: '@user'}. Where is this @user object supposed to be? It would be much appreciated if someone explained what's going on here.

Thanks!

È stato utile?

Soluzione

From the documentation:

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

And later on they have an example

var User = $resource('/user/:userId', {userId:'@id'});

So the second argument to the resource service are the default parameters. In this case, by default, the userId will be extracted from the resource object you call a method on.

Previously I had an example of a GET operation, which doesn't really make sense in this case. But imagine you were doing a POST ($save) request with a data object. Angular would automatically extract the userId from the data object and inject it into the URL for you. You don't have to manually specify the value for the URL.

More info can be found on the doc page.

http://docs.angularjs.org/api/ngResource.$resource

Altri suggerimenti

Quoting the documentation: "If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations)."

The '@user' tells Angular to extract the value of user_id from the 'user' property of the object. So if you call .save() on an object that has an 'user' property of 'abc123', then the :user_id in your URL will be replaced with 'abc123'.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top