Restangular post incorrectly created a singular of wage_schedules

23 Views Asked by At

I'm using restangular with AngularJS 1.5 and Rails 5.x and have the following;

Restangular.all('wage_schedules').post($scope.wage_schedule).then (response) ->
  $state.go "wage_schedules"

When this request gets to my Rails backend the params includes "wage_schedul" and it's missing the 'e' on the end. At first I thought it was Ruby on Rails and the inflection file but after playing around with that it still didn't work.

Is Restangular changing my wage_schedules to wage_schedul and how would I train it to correctly singularise 'wage_schedules' to 'wage_schedule'?

When I check my payload sent to the server from Restangular I can see that it's already called 'wage_schedul' without the 'e' on the end.

If I swap the syntax to

Restangular.all('jobs').post($scope.wage_schedule).then (response) ->
  $state.go "wage_schedules"

Then I get job: {} in my post payload. So Restangular must be misinterpretting the conversion of wage_schedules to wage_schedule.

1

There are 1 best solutions below

0
map7 On

The answer was to use a package: https://github.com/blakeembrey/pluralize and configure it to be used with Restangular.

angular
  .module('paisApp')
  .config ['RestangularProvider', (RestangularProvider) ->
    RestangularProvider.setRequestInterceptor( (elem, operation, what) ->
      retElem = elem
      if operation == 'post' || operation == 'put'
        wrapper = {}
        singular = pluralize.singular(what)

        # Check if we have already applied a root node
        if elem[singular] == undefined
          wrapper[singular] = elem
          retElem = wrapper
      return retElem
    )
  ]