Make Restangular .remove() work with Rails Shallow Routes

Issue

Say, I have nested resources as follows,

shallow do
  resources :aalu do
    resources :pyaj
  end
end

Using restangular I get the resources as required using the following Restangular call

all_pyaj = Restangular.one('aalu', $stateParams.aaluId).all('pyaj').getList();

pyaj is an element of the all_pyaj array, I get it using ng-repeat.

Now, When I want to remove the resource using Restangular using remove() method,
the pyaj.remove() makes a DELETE call to /aalu/:aalu_id/pyaj/:pyaj_id

I want it to make a DELETE call to /pyaj/:pyaj_id and remove the resrouce form the angular $scope too.

I can achieve this by the following way,

Restangular.one('pyaj', payj.id).remove().then(
   function (response) { /* Problem: Delete this pyaj from $scope */ }

Can I make Restangular make a DELETE call to /pyaj/:pyaj_id and remove the resource form the angular $scope too?

How Restangular works with shallow routes?

Solution

You should configure Restangular with RestangularProvider.setParentless(['pyaj']); to tell it that any pyaj object should be “shallow”. See https://github.com/mgonto/restangular#setparentless.

Answered By – Meekohi

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.