AngularJS到Rails MassAssignmentSecurity ::错误

时间:2013-12-17 04:01:29

标签: javascript ruby-on-rails angularjs

在使用带有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']}"

2 个答案:

答案 0 :(得分:1)

也许您应该考虑允许使用Clientattr_accessible模型上的属性进行质量分配?

答案 1 :(得分:0)

但是,您要分配属性,您需要在模型中将它们声明为可批量分配。请尝试以下方法:

class Client < ActiveRecord
    attr_accessible :created_at, :updated_at # Any attributes you need to make mass-assignable
end