Angular JS ngResource with nested resources

GTDev picture GTDev · Feb 25, 2013 · Viewed 13.7k times · Source

I'm trying out angular JS and I want to get data from a nested resource defined in my rails application.

I wrote the following lines:

UserMission = $resource("/users/:user_id/user_missions/:id", {user_id: "@user_id", id: "@id"}, {update: {method: "PUT"}})
$scope.user_missions = UserMission.query()

and I get the following error:

  Processing by UsersController#show as JSON
  Parameters: {"id"=>"user_missions"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "user_missions"]]
Completed 404 Not Found in 10ms

ActiveRecord::RecordNotFound (Couldn't find User with id=user_missions):
  app/controllers/users_controller.rb:100:in `current_resource'
  app/controllers/application_controller.rb:34:in `authorize'

My rails routes are organized like so:

resources :users do
  resources :user_missions
end

I think it comes down to me not understanding "@id". It says it comes off of the "data object" from the angularjs site and I am not exactly sure what that means.

Any help would be appreciated thanks.

Update

Another question. I have not found any examples of rails with angularjs using nested resources (an example User has_many :missions, through: :user_missions ) with $resource. Is there a good example of angularjs manipulating nested resources (with $resource) with ajax?

Answer

remigio picture remigio · Feb 25, 2013

Read through my answer to your previous question, you should include the values for both the parameters in an object you pass as a parameter in the call, i. e.:

$scope.user_missions = UserMission.query({user_id: some_value, id: some_other_value});