我创建了一种通过 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
routes.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'