Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using angularjs-rails-resource , in my Rails Angular App.

Account Resources

myApp.factory('Account', ['railsResourceFactory','railsSerializer', function                          
  (railsResourceFactory,railsSerializer) {
    return railsResourceFactory({
     url: '/accounts',
     name: 'account',
     serializer: railsSerializer(function () {
       this.nestedAttribute('address');
    })
  });
}]);

UserController.js

function userController($scope,$location,Auth,$rootScope,$http,Useraccount,Account) {
  $scope.profileUpdate = function() {

   //Useraccount.save(); // THIS WORKS

   $scope.account = {}
   $scope.account.save()  // Throwing error : undefined function save
 }
}

UserAccount Service

myApp.service('Useraccount',function(Auth,$location,$rootScope,Account){
 var account;
 var query = function(){
 var promise = Account.query().then(function (results) {
   account = results;
 }, function (error) {
   alert("Went Wrong while fetching User Account!!")
 });

 return promise;
}

var save = function() {
  account.save().then(function (results) {
   console.log(results);
  }, function (error) {
    alert("Went Wrong!!")
  });
 }
return {
   query:query,
   save:save
  }
 })
});

I am not sure why the save function from UserController is not working though I have imported Account resources as dependency. I did same in service , but it was working there. Any clue will be helpful.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You are actually calling the save() method for an empty javascript object. I don't see the point here.

Anyway you need an Angular object to do so. So either load account data from server.

$scope.accounts = Account.query(); // Will be an Array of accounts

Or create new instance of Account

$scope.account = new Account(); // An empty object
share|improve this answer
    
Thanks for prompt help, it my Mistake , I misplaced the line. –  Senthil Sep 10 at 5:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.