0

当页面加载时,资源会抛出 404,因为 $resource 正在读取 nil for :city_id。我从 angularjs 开始,所以任何澄清都值得赞赏。

表单条目无法保留,因为此 $resource 还用于 PUT 模型中的条目。

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 = {}
]
4

1 回答 1

2

当您提到 rails 时,我建议您查看angularjs-rails-resource。它使使用资源变得更加容易。当您将海豹嵌套在 city 下方时,您始终必须提供 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 = {}
]
于 2013-02-26T12:38:29.753 回答