Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So here's a weird one:

I'm using the ui.router with Restangular in AngularJS, and when I want to get a specific resource I can do:

Restangular.one('books', $stateParams.id)

and then in rails:

@book = Book.find( params[:id] )

and that works.

'

But if I use:

@book = Book.where(id: params[:id])

that doesn't work.

this has something to do with params in rails and $stateParams, and how AngularJS is talking to rails, but I'm not sure what's really going on. I want to be able to say:

@pages = Page.where(book_id: params[:id]) 

so I can access the pages of book 2 or whatever.

share|improve this question
    
Book.where(id: params[:id]).first –  apneadiving Jul 15 '14 at 6:15
    
hmmm, that DOES work for the parent resource... but I really need to get at ALL the nested resources, in this case all the pages that belong to a certain book. –  user3583384 Jul 15 '14 at 18:07

1 Answer 1

To be clear about the interaction between Restangular and Rails, run rake routes and look for a route like this:

api/v1/books/1/pages

If it's not there then set up the routes.rb for the API:

namespace :api do
  namespace :v1 do
    resources :books do
      resources :pages
    end 
  end
end

Then use a Rails debugger (byebug on Rails 4) to put a breakpoint in the Pages controller and look at the params. Also use the JS debugger in chrome to see what is returned. Look in the Rails log to see what route Restangular is using.

If the pages controller's index method is called, you could check for a book_id and search for pages with that book_id and respond with them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.