2013-02-22 57 views
2

嵌套模型驗證我創建通過JSON驗證嵌套模型的方法,但它給我以下錯誤:失敗在通過JSON

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

這是我在其他問題在這裏看到的錯誤所以,但他們都沒有幫助解決我的問題。我不知道該怎麼辦,下面我的方法和模型:

表(模型):

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' 
+0

你是如何發送JSON字符串?我的意思是,你在javascript方法中手動構建它還是序列化表單? – 2013-02-22 19:06:40

+0

我用這個信息更新了這個問題,我使用了一個HttpRequest並按照我在問題中顯示的方式發送。 – 2013-02-22 19:32:06

+0

好吧,那麼它似乎是在你的視圖文件中的'fields_for'問題,它假設是'table_fields_attributes'而不是'table_fields'。可能會添加視圖代碼可以提供幫助。對不起,在第一條評論中不添加此部分:) – 2013-02-22 19:47:26

回答

1

要麼改變ATTR訪問聲明使用table_fields代替table_fields_attributes或改變JSON到table_fields_attributes元送table_fields

代替
attr_accessible :x, :y, :name, :table_fields_attributes 

o [R

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

要麼應該工作

+0

謝謝,實際上這個答案幫助我找到了一種解決方法,但我不確定這是否是正確的方法。我將attr_acessible更改爲'attr_accessible:x,:y,:name,:table_fields_attributes,:table_fields',可以這樣做嗎? – 2013-02-22 21:41:51

+0

@EduardoCopat這很好,它不會引起任何問題,但如果只是爲了更熟悉json的工作方式,最好更改json – jamesc 2013-02-22 22:09:03