0

so... I want to get the nested resources of a parent resource.

I want to show all the PAGES of a certain BOOK.

It would be nice to do it with restangular somehow. It seems like I ought to be able to when I look at the docs. Here is a hack I've made in angularJS that works, but I feel like I should be making this happen with Restangular:

$scope.getPages = () ->
      Restangular.one('books', 8).getList('pages').then (response) ->
        console.log(response)
        $scope.pages = $filter('filter')(response, book_id: 8)

Restangular.one('books', 8).getList('pages') returns ALL the pages from ALL the books. I don't get that.... shouldn't it return just the pages from book 8?

Anyway, from there I'm just filtering the pages so I only end up with the pages from book 8.

But can't I do this with Restangular somehow?

PS, There are no examples I've found that use the $filter from AngularJS in a js file like this... everything is done through HTML like "page in pages | filter: {book_id: 8}"

1
  • Did my answer solve your problem..? Commented Jul 17, 2014 at 16:46

1 Answer 1

2

Your syntax is a little wrong so the wrong request is probably being sent. Try:

Restangular.one('books', 8).all('pages').getList().then(response)

This should make a request to: /books/8/pages

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

2 Comments

Thanks!! In reality.. both of those syntaxes make the right GET request. I had my rails Controller giving back Book.all instead of Book.find(params[:id]). As a side note... if I wanted to make a GET request for pages that belonged to a book... I would say @pages = Page.where(book_id: params[:book_id]) and not Page.where(book_id: params[:id]). So really, my problem was getting Angular to talk to rails correctly
@user3583384 The syntax is definitely not correct and both of these will not make the same GET request. The getList('pages') in your question is incorrect, only query params should go in the get and getList parts of the builder. What you have in your question will make a request to books/8.

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.