0
我有一個recipe
窗體,也是has_many
成分。通過一些(跑題了)出來的模式選擇,我最終看起來像這樣被傳遞到「食譜」控制我的「創造」動作參數:在Rails中手工創建嵌套模型3
{
"commit" =>"Create Recipe",
"authenticity_token" =>"qIeydCyOetcmu2bba3BM9X7HSXXtiFt7cuyuK7yvTPc=",
"utf8" =>"✓",
"recipe" => {
"title" =>"",
"url" =>"",
"site" =>"",
"instructions" =>"",
"ingredients_attributes" => {
"0" => {
"quantity" =>"5",
"name" => "flour"
}
}
}
}
我試圖錘成這個一個模型可以一次性保存所有內容,但無法從表單構建器中獲益。我嘗試過各種方法,當我撥打@recipe.save
時,將其保存到我的模型中,但似乎總是被忽略。我嘗試將它放入模型期望的數組中,但這也不起作用。這裏是控制器代碼我一直在瞎搞用:
def create
arrayIngredients = [];
params[:recipe][:ingredients_attributes].values.each do |value|
arrayIngredients.push value
end
@recipe = Recipe.new(params[:recipe])
@recipe.ingredients.build = arrayIngredients
# at this point this is [{"name"=>"flour"}] and so forth
@recipe.user_id = current_user.id
@recipe.title = @recipe.inspect # just debugging purposes
respond_to do |format|
if @recipe.save... # do stuff
我覺得我對這個很接近,但我缺少一個關鍵的事情,使這個得到妥善保存(目前在終端窗口是在所有INSERT語句中顯示NULL)
感謝您的任何輸入!
嗯,我已經有那些在模型中設置......什麼都可能是錯了嗎? .build是必要的嗎? –