【发布时间】:2013-12-17 04:01:29
【问题描述】:
在使用带有 Rails 的 AngularJS 时,我在更新时不断收到 MassAssignmentSecurity 错误。我知道这是由于将“created_at”和“updated_at”等属性与数据一起传递。
为了解决这个问题,我一直在构建一些 json,它只是传递表单中的属性。这确实会在整个程序中产生更多的维护。
有没有更好的方法来做到这一点?
这是一个例子:
AngularJS
$scope.contruct_json = ->
{
name: $scope.client.name
surname: $scope.client.surname
}
# --------------------------------------------------------------------------------
# Update
# --------------------------------------------------------------------------------
$scope.update = ->
Client.update
id: $stateParams['id']
,
client: $scope.contruct_json()
, (response) ->
$location.path "/clients/#{$stateParams['id']}"
更新
将我的 AngularJS 代码更改为此
# Remove keys from hash to make it acceptable for Rails to update
$scope.remove_keys = (hash) ->
new_hash = {}
angular.forEach(hash, (value,key) ->
if(key!='id' && key!='created_at' && key!='updated_at')
new_hash[key]=value
, new_hash)
return new_hash
# --------------------------------------------------------------------------------
# Update
# --------------------------------------------------------------------------------
$scope.update = ->
Client.update
id: $stateParams['id']
,
client: $scope.remove_keys($scope.client)
, (response) ->
$location.path "/clients/#{$stateParams['id']}"
【问题讨论】:
标签: javascript ruby-on-rails angularjs