How to use PUT method in restangular

Binson Eldhose picture Binson Eldhose · Dec 21, 2014 · Viewed 14.8k times · Source

I am using restangular, but I have a problem with "Put" method, its not working as expected

My angularService code

 var userService = function (restangular) {
            var resourceBase = restangular.all("account/");

            restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
                if (operation == "getList") {
                    return response.data;
                }
                return response;
            });
      this.getUserById = function (id) {

                return resourceBase.get(id);
                // return restangular.one("account", id).get();
            };
            this.updateUser = function(user) {
                return user.Put();
            };
}

My controller code

 var userEditController = function (scope, userService, feedBackFactory, $routeParams) {

        scope.user = undefined;

        scope.updateUser = function () {

            userService.updateUser(scope.user).then(function (data) {
                feedBackFactory.showFeedBack(data);
            }, function (err) {
                feedBackFactory.showFeedBack(err);
            });
        };

        userService.getUserById($routeParams.id).then(function (data) {
            scope.user = data.data;   **// Please not here I am reading the object using service and this object is getting updated and pass again to the service for updating** 

        }, function (er) {

            feedBackFactory.showFeedBack(er);
        });

    };

but I am getting an error "Put" is not a function , I checked the user object and I found that the user object is not restangularized ( no any additional methods found). How can I solve it

Answer

Paul Smith picture Paul Smith · Apr 13, 2015

You can only 'put' on a data object.

customPUT([elem, path, params, headers]) is what you want. use it like this:

Restangular.all('yourTargetInSetPath').customPUT({'something': 'hello'}).then(
  function(data) { /** do something **/ },
  function(error) {  /** do some other thing **/ }
);