我正在尝试通过CreditNote更新LineItem。这是一个API,所以我试图通过JSON更新。
我的关系模型是:
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 :(得分: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