[英]API: Update model through an other related model
我正在嘗試通過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 :: Error:無法批量分配受保護的屬性:line_items
我添加了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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.