[英]RoR - Processing partials from different models within one form?
我正在嘗試構建一個 model 來渲染/處理來自其他模型的部分。 基本上,主 model 將提供一些通用功能,同時根據用戶輸入嵌入不同的模型。
換句話說,在同一個 model 中,我需要從各種模型中渲染“_form.html.erb”並將它們保存為嵌套的 model 即可。
我知道如果嵌套的 model始終是相同類型的 model,它會完成這項工作。
class Master < ActiveRecord::Base
has_one :{change depending on user input}
accepts_nested_attributes_for :{change depending on user input}
end
我看到了多態模型,但它似乎完全相反(即多個模型中的一個部分)。
有任何想法嗎? 多態嵌套 model 還是什么?
看起來你在這里肯定需要一個多態關聯,但是accepts_nested_attributes_for 不支持多態。
但是,看看這個相關的問題Accepts_nested_attributes_for with belongs_to polymorphic
也許在您的表單中,您可以執行以下操作來呈現正確的 _form 部分:
<%= render :partial => File.join(@master.thing.class.name.underscore.pluralize, "form") %>
祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.