![](/img/trans.png)
[英]STI in Rails: How do I change from a superclass to a subclass without accessing the “type” attribute directly?
[英]Rails / STI - How do I show a different form based on the type?
從同一模型繼承3種不同的類型。 每個屬性都有一個與其他屬性無關的唯一屬性。
如果每種類型都有鏈接,是否可以使用相同的表格,但是可以動態顯示每種類型的相關字段?
還是我必須為每個表單創建不同的表單並使用隱藏字段來告訴控制器要創建哪種類型?
我假設您要根據要顯示的隱藏字段在查詢字符串中傳遞變量
如果是這種情況,這就是我會做的
= simple_form_for(@model, :html => {:class => 'form-horizontal' }) do |f|
...
- if params[:what_hiddind_filld_to_show] == 'type1'
= f.input :fild_name, as: :hidden, , input_html: { value: '1' }
- elsif params[:what_hiddind_filld_to_show] == 'type2'
= f.input :fild_name, as: :hidden, , input_html: { value: '2' }
- elsif params[:what_hiddind_filld_to_show] == 'type3'
= f.input :fild_name, as: :hidden, , input_html: { value: '3' }
- else
= f.input :fild_name, as: :hidden, , input_html: { value: 'default' }
我希望這能使您走上正確的道路
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.