AngularJS to Rails MassAssignmentSecurity::Error
AngularJS to Rails MassAssignmentSecurity::Error
在Rails中使用AngularJS时,我在更新时不断收到MassAssignmentSecurity错误。我知道这是由于将诸如"created_at"answers"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']}"
也许您应该考虑允许使用attr_accessible
为Client
模型上的属性进行批量分配?
无论您如何分配属性,都需要在模型中将它们声明为可批量分配。尝试以下操作:
class Client < ActiveRecord
attr_accessible :created_at, :updated_at # Any attributes you need to make mass-assignable
end
相关文章:
- jQuery Error adadvisor.net
- Ajax调用ERror Cross Origin REquest:在rails中自动完成大学列表
- 类型error primeFactors.for不是函数
- Array.every返回false,而不是类型error
- datatables.net ajax error - http://datatables.net/tn/7
- Error: timers.js:234 callback.apply(timer, args);
- 滚动到错误字段ERROR
- Error with xmlhttpresponse
- ajax javascript 500 error
- 将数据从ExpressJS发送到AngularJS ERROR json
- walkscore API JSON error
- STRIPE.JS Error
- Angular Routing Pretty URL Error
- 节点回调参数无法断言instanceof Error
- Jasmine .calls.count and .calls.any throwing error
- React.render error
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- grunt contrib jshint-error在定义之前就已经使用过了
- Javascript getElementById null error
- CreateJS - d.mousedown.f error on hitAreas