4

Based on my understand from the restangular docs, calling Resource.getList('subElement',{query:param}) should make a call to /resource/subelement?query=param

I am using Ransack on top of a rails API, and I am attempting to pass some search parameters, which take the form "q[field_eq]=foo".

Here's a controller:

.controller('PlaypenCtrl', function PlaypenCtrl(
  $scope,
  Resource
) {
  'use strict';

  var qParams;

  $scope.doIt = function() {
    qParams = {
      per_page: 10
    };
    qParams['q[some_field_eq]'] = 'foo';
    console.log(qParams); //Object {per_page: 10, q[some_field_eq]: "foo"}

    Resource.getList('subElement',qParams).then(function(list){
      console.log(list);
    });

  };
});

When I call doIt, this gets logged:

Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'q[some_field_eq]' is not a valid HTTP header field name.

This makes sense, because obviously that is not a valid HTTP header field name.

The question is... why would Restangular be attempting to put the qParams object into the request headers? These should be request parameters, according to the docs.

If I take out the 'subElement', things work as expected:

  $scope.doThat = function() {
    qParams = {
      per_page: 10
    };
    qParams['q[some_field_eq]'] = 'foo';
    console.log(qParams); //Object {per_page: 10, q[some_field_eq]: "foo"}

    Resource.getList(qParams).then(function(list){ 
      //calls /resource?per_page=10&q%5Bsome_field_eq%5D=foo
      console.log(list); //...a normal restangular list object, as expected
    });
  };

What am I missing?

1 Answer 1

2

The two parameters to the getList method are the query parameters and headers. In order to access the subElement endpoint you must use the following:

Resource.all('subElement').getList(qParams)

I believe that is what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.