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.

When the page loads, the resource throws a 404 because the $resource is reading nil for :city_id. I am beginning with angularjs so any clarification is appreciated.

The form entries fail to persist because this $resource is also used for PUT'ing the entries in the model.

app = angular.module("CityAngular", ["ngResource"])
app.factory "Seal", ["$resource", ($resource) ->
    $resource("/cities/:city_id/seals/:id", {city_id: "@city_id", id: "@id"}, {update: {method: "PUT"}})
]

@SealCtrl = ["$scope", "Seal", ($scope, Seal) ->
    $scope.seals = Seal.query()

    $scope.addSeal = ->
        seal = Seal.save($scope.newSeal)
        $scope.seals.push(seal)
        $scope.newSeal = {}
]
share|improve this question

1 Answer 1

up vote 2 down vote accepted

As you mention rails, I suggest you look at angularjs-rails-resource. It makes working with resources much easier. As you are nesting your seals below city, you always have to supply a city_id.

app = angular.module("CityAngular", [])
app.factory "Seal", ["railsResourceFactory", ($resource) ->
  $resource url: "/cities/{{city_id}}/seals/{{id}}", name: 'seal'
]

app.controller 'SealCtrl', ["$scope", "Seal", ($scope, Seal) ->
  $scope.city_id = 1

  $scope.seals = Seal.query(city_id: $scope.city_id)

  $scope.addSeal = ->
    $scope.newSeal.city_id = $scope.city_id
    seal = new Seal($scope.newSeal).create()
    $scope.seals.push(seal)
    $scope.newSeal = {}
]
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.