2012-03-22 32 views
0

我正在使用backbone並嘗試創建一個新對象,並且遇到此錯誤。我認爲rails會忽略它不關心的屬性。即真實性標記未嘗試添加到我的對象。Backbone/Rails - build(params [...])引發ActiveRecord :: UnknownAttributeError

我的控制器:

def create 
    page = Page.find(params[:page_id]) 
    branch = page.page_branches.build(params[:page_branch]) 
    branch.form = page.form 
    if branch.valid? 
    page.page_branches << branch 
    redirect_to(edit_page_path(page, :anchor => "branch-panel")) 
    else 
    raise "#{branch.errors.map}" 
    end 
end 

的錯誤:

Processing by PageBranchesController#create as JSON 
Parameters: {"page_branch"=>{"next_page_id"=>"KS5ad82889e04e5806e0455d5a81e81a9511", 
"description"=>"linking", "utf8"=>"", "authenticity_token"=>"", "id"=>nil, 
"page_id"=>"KS000c29724a16-u-2TQMP57-gYm-U", "trigger_type"=>"Always", 
"base_options"=>"AllowAnonymous;BASE", 
"question_options"=>"KS000c29724a16CfC2TQUL1--gxG-U", "keyword_options"=>"", 
"qualification"=>"", "commit"=>""}, "page_id"=>"KS000c29724a16-u-2TQMP57-gYm-U"} 
WARNING: Can't mass-assign protected attributes: id 
Completed 500 Internal Server Error in 239ms 

ActiveRecord::UnknownAttributeError (unknown attribute: utf8): 
    app/controllers/page_branches_controller.rb:21:in `create' 

得益於以下回應:

我說我的軌道模型attr_accessible:

attr_accessible :page_id, :page_name, :next_page_id, :next_page_name, 
       :description, :trigger_type, :internal_qualification, 
       :order, :form_id, :qualification 

通過做th在,我得到以下警告,但生成成功

WARNING: Can't mass-assign protected attributes: utf8, authenticity_token, id, base_options, question_options, keyword_options, commit 

回答

1

ActiveRecord的質量分配是要去嘗試使用哈希每個鍵/值對提供和提高當UnknownAttributeError unknown attributes are supplied via mass assignment

也許檢出this code可以幫助確認我在說什麼。

我認爲你有兩個選擇:利用它在質量分配

  1. 要清理params[:page_branch]
  2. 修改Backbone.YourModel.toJSON()以不發送奇怪的屬性到服務器。
相關問題