2013-11-22 54 views
0

更新模型我試圖使人們有可能更新是LineItem海槽CreditNote。這是一個API,所以我試圖通過JSON來更新。API:通過其他相關模型

我的關係模型是:

class TestCreditNote < ActiveRecord::Base 
    self.table_name = :credit_notes 
    has_many :line_items, :class_name => TestLineItem, :foreign_key => :artef_id 
    accepts_nested_attributes_for :line_items 
end 

class TestLineItem < ActiveRecord::Base 
    self.table_name = :line_items 
    attr_accessible :description 
    belongs_to :credit_note, :class_name => TestCreditNote, :foreign_key => :artef_id 
end 

當執行這個測試:

it "should update the sales line item record" do 
    put "api/v1/credit_notes/#{@credit_note.id}", { :test_credit_note => { :line_items => [{ :description => 'PEPITO'}] }}, http_headers 
    data = JSON.parse(response.body, :symbolize_names => true) 
    TestCreditNote.find(@sales_credit_note.id).line_item.description.should == 'PEPITO' 
    end 

它失敗的原因爲: ::加載ActiveModel :: MassAssignmentSecurity錯誤: 不能大規模指派保護屬性:line_items

回答

0

我添加了attr_accesible:line_items_attributes

class TestCreditNote < ActiveRecord::Base 
    self.table_name = :credit_notes 
    has_many :line_items, :class_name => TestLineItem, :foreign_key => :artef_id 
    accepts_nested_attributes_for :line_items 
    attr_accessible :line_items_attributes 
    end 

而且在測試中相同

it "should update the sales line item record" do 
    put "api/v1/credit_notes/#{@credit_note.id}", { :test_credit_note => { :line_items_attributes => [{:id => 1, :description => 'PEPITO'}] }}, http_headers 
    data = JSON.parse(response.body, :symbolize_names => true) 
    TestCreditNote.find(@sales_credit_note.id).line_item.description.should == 'PEPITO' 
end 
相關問題