ruby-on-rails - 通过JSON进行的嵌套模型验证失败

标签 ruby-on-rails json dart

我创建了一种方法来通过JSON验证嵌套模型,但它给了我以下错误:

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes:  table_fields):

我在SO的其他问题中看到了这个错误,但是没有一个问题可以解决我的问题。按照我的方法和模型,我不知道该怎么办:

表(模型):
class Table < ActiveRecord::Base
  attr_accessible :x, :y, :name, :table_fields_attributes

  validates :name, :presence => :true

  has_many :table_fields
  accepts_nested_attributes_for :table_fields, :allow_destroy => true
end

表字段(模型)
class TableField < ActiveRecord::Base
  attr_accessible :foreign_key, :name, :primary_key, :data_type, :table_id
  belongs_to :table

  validates :name, :presence => :true
end

验证table_controller中的方法
  # POST /table.json/validate
  def validate
    @table = Table.new(params[:table])

    respond_to do |format|
      if @table.valid? == false
        format.json { render json: @table.errors, status: :unprocessable_entity }
      else
        format.json { head :no_content }
      end
    end
  end

路由的部分。rb
  resources :tables do
    resources :table_fields
  end
  match "/table.json/validate" => "tables#validate"

JSON我正在发送
{"table":{"y":5,"name":"","x":5,"table_fields":[{"table_field":{"data_type":"CHAR","primary_key":true,"foreign_key":false,"name":""}}]}}

涉及HttpRequest(Dart)的代码
  void validate(Table table)
  {
    HttpRequest req = new HttpRequest(); // create a new XHR
    String url = "/table.json/validate";
    req.open("POST", url); // Use POST http method to send data in the next call]
    req.setRequestHeader("Content-Type", "application/json");
    String tableJson = table.toJson();
    req.send(tableJson); // kick off the request to the server
  }

类表
class Table{
  int tableId;
  List<TableField> tableFields;
  String name;

  num x;
  num y;
  num width;
  num height;

  Table(String name, num x, num y, List<TableField> tableFields)  {
    this.name = name;
    this.x = x;
    this.y = y;
    this.tableFields = tableFields;

  }


  toJson()
  {
    Map map = new Map();
    map["table"] = new Map();
    map["table"]["name"] = this.name;
    map["table"]["x"] = this.x;
    map["table"]["y"] = this.y;

    List<Map> mappedFields = new List<Map>();
    Map fieldMap = new Map();
    fieldMap["table_field"] = new Map();

    //Testing with 1 entry only
    TableField tableField = tableFields[0];

     fieldMap["table_field"]["name"] = tableField.name;
     fieldMap["table_field"]["data_type"] = tableField.dataType;
     fieldMap["table_field"]["primary_key"] = tableField.primaryKey;
     fieldMap["table_field"]["foreign_key"] = tableField.foreignKey;
     mappedFields.add(fieldMap);


    map["table"]["table_fields_attributes"] = mappedFields;

    return stringify(map);


  }


}

完整错误堆栈
Started POST "/table.json/validate" for 127.0.0.1 at 2013-02-22 15:17:00 -0300
Processing by TablesController#validate as */*
  Parameters: {"table"=>{"y"=>5, "name"=>"", "x"=>5, "table_fields"=>[{"table_field"=>{"data_type"=>"444", "primary_key"=>true, "foreign_key"=>false, "name"=>""}}]}}
WARNING: Can't verify CSRF token authenticity
Completed 500 Internal Server Error in 2260ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: table_fields):
  app/controllers/tables_controller.rb:88:in `new'
  app/controllers/tables_controller.rb:88:in `validate'

最佳答案

更改attr可访问声明以使用table_fields而不是table_fields_attributes或更改json以发送table_fields_attributes元素而不是table_fields

attr_accessible :x, :y, :name, :table_fields_attributes

要么
{"table":{"y":5,"name":"","x":5,"table_fields_attributes":[{"table_field":{"data_type":"CHAR","primary_key":true,"foreign_key":false,"name":""}}]}}

都应该工作

关于ruby-on-rails - 通过JSON进行的嵌套模型验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030901/

相关文章:

ruby-on-rails - 如何在Rails上使用Tyre gem检查字段是否为空或为空

asp.net-mvc - 如何在 Json .NET Web API 中 POST 但 JsonIgnore GET 时读取属性

javascript - Chart.js 圆环图无法正常工作

ruby-on-rails - rspec 模拟对象属性赋值

ruby-on-rails - 如何在 Rails Controller 操作的 RSpec 测试中指定查询字符串?

javascript - 比较来自 JSON 的信息,返回列表中的顶部结果

user-interface - 有所见即所得的 Dart 编辑器吗

dart - 如何在 Flutter 中使用共享首选项

dart - dart-从子句返回函数值

ruby-on-rails - 自定义分析器 elasticsearch-rails