我知道我们可以使用fields_for为嵌套属性创建一个字段的子部分。不过,我想通过表格将它们分开。我怎样才能做到这一点?Rails - 如何通过窗体拆分嵌套属性?
例如:
假设我有一个模型foo且嵌套酒吧的模式,就像这样:
class Foo < ApplicationRecord
has_many :bars
accepts_nested_attributes_for :bars
end
一个普遍的看法是这样的:
<%= form_for @foo do |f| %>
<!-- foo fields -->
<%= f.fields_for :bars do |f_bar| %>
<!-- bar fields -->
<% end %>
<%= f.submit "Submit" %>
<% end %>
但出于美学的原因,我不希望所有的bars
集中在一个地方。我想这样做:
<%= form_for @foo do |f| %>
<!-- foo fields -->
<%= f.fields_for :bars do |f_bar| %>
<!-- bar fields of one bar -->
<% end %>
<!-- other foo fields -->
<%= f.fields_for :bars do |f_bar| %>
<!-- bar fields of another bar -->
<% end %>
<!-- The previous repeats many more times in a non predictable way -->
<%= f.submit "Submit" %>
<% end %>
因此,这将是完美的我,如果我没有来显示所有的bars
一次。有人知道如何做到这一点?
你试过了吗? –
您可以尝试像使用'@ foo'完成的那样传递实例变量。将单条滤除为一个实例变量,并放在控制器中的另一个变量中,您可以在视图中使用该变量。 – vee
单个变量的数量未确定。这只是一个例子.. –