【问题标题】:Submitting Form with Angularjs-rails throws error because of Angular Resource由于 Angular 资源,使用 Angularjs-rails 提交表单会引发错误
【发布时间】:2013-02-26 21:47:07
【问题描述】:

当页面加载时,资源会抛出 404,因为 $resource 为 :city_id 读取 nil。我从 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 = {}
]

【问题讨论】:

    标签: ruby-on-rails coffeescript angularjs angular-resource


    【解决方案1】:

    当您提到 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 = {}
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多