2012-11-15 44 views
0

這裏是我的模型;用Rails創建帶嵌套模型的模型JSON請求導致MassAssignmentSecurity

class Calendar <ActiveRecord::Base 
attr_Accessible :email, :cal_items_attributes 
has_many :cal_items 
accepts_nested_attributes_for :cal_items 
end 

class CalItem < ActiveRecord::Base 
attr_accessible :calendar_id, :name, :url 
has_one :calendar 
end 

我的網站將是一個主幹應用程序,所以我做一個POST請求到localhost:3000/calendars.json作爲

{"calendar": 
    {"email":"[email protected]", 
    "cal_items": 
    [{"url":"http://www.google.com"},{"url":"http://www.yahoo.com"}] 
    } 
} 

,並返回

加載ActiveModel

的錯誤: :MassAssignmentSecurity :: CalendarsController中的錯誤 #create 無法批量分配受保護的屬性:cal_items

I認爲attr_accessible與cal_items_attributes佔了!

謝謝, 喬

回答

2

你有attr_accessiblecal_items_attributes,但你在JSON用於cal_items。

嘗試:

{"calendar": 
    {"email":"[email protected]", 
    "cal_items_attributes": 
    [{"url":"http://www.google.com"},{"url":"http://www.yahoo.com"}] 
    } 
} 
+1

你也不必'attr_accessible:cal_items_attributes'爲'accepts_nested_attributes_for:cal_items'已經將其添加 –