在我的Rails應用程序中,我使用了多次使用fields_for
的表單,有時會將調用嵌套到fields_for
。RoR - 調用fields_for次數可變的幫助程序方法
不幸的是,圖形設計師堅持要求輸入字段的排序方式不會根據它們所屬的模型進行分組。因此,我想成爲一名助手,可以輕鬆地爲我構建fields_for
塊,但我看不到如何讓輔助方法嵌套可變數量的塊。
爲了說明,我設想的代碼看起來像以下:
<%= form_for @object do |f| %>
<!-- The helper method should have a similar effect to the following... -->
<%= f.fields_for :assoc_a do |assoc_a_builder| %>
<%= assoc_a_builder.fields_for :assoc_b do |assoc_b_builder| %>
<%= assoc_b_builder.text_field :field_name %>
<% end %>
<% end %>
<!-- ...when given the following data... -->
<%= my_helper [f, :assoc_a, :assoc_b], :text_field, :field_name %>
<% end %>
任何想法如何,我可以做到這一點?
你重歸於好? – ardochhigh
請參閱下面的答案。 – JellicleCat