2
我已經看到了使用accepts_nested_attributes_for的例子很多,但僅適用於JSON POST/PUT他們幾個人,沒有他們的幫助我:/accepts_nested_attributes_for蒙山JSON PUT(更新)
我的應用程序將被用於創建表單。
所以,一個表單有很多form_row,而form_row可以有很多選擇(如果它是一個radio的選擇)。 所以,我有我的表單模型是這樣的:
class Form < ActiveRecord::Base
attr_accessible :name
has_many :form_rows
accepts_nested_attributes_for :form_rows
end
和我的控制器看起來像這樣:
def update
@form = Form.find(params[:id])
@form.update_attributes!(params[:form])
end
這裏是JSON我想送
{
"name": "form test 4",
"form_rows_attributes": [
{
"domtype": "Input",
"label": "Super row new"
}
]
}
在我場景中,表單創建只需要一個名稱,然後是用戶添加form_rows。
當我這樣做時,名稱被正確更新,但form_rows根本沒有創建。我的控制檯中沒有錯誤,只是表單的UPDATE。
我在做什麼錯?
我已經試過了,但只有名稱被更新。不添加form_rows:/ – Olivier 2013-03-04 08:59:27
Oups抱歉,它的工作原理,我做錯了。不過,我仍然有問題。我form_row可以有很多選擇,但這樣的{ 「形式」 一個JSON:{ 「名」: 「形式測試4」, 「form_rows_attributes」: { 「domtype」: 「輸入」, 「標籤」 : 「超級排新」, 「選擇」: { 「名」: 「選擇1」, 「值」: 「VALUE1」 } ] } ] } } 不工作: /我有一個錯誤: ActiveModel :: MassAssignmentSecurity :: FormsController中的錯誤#update 無法整體分配受保護的屬性es:choices – Olivier 2013-03-04 11:44:53
@Tagazok你在'FormRow'模型裏面有'attr_accessible:choices'嗎?你的'FormRow'模型中還需要'accep_nested_attributes_for:choices'。 – patrickmcgraw 2013-03-04 17:51:16