我有一个真正的野兽表单,它本质上是一个五页的销售表单,在创建新表单时一切都很好。但是当您去编辑新表单时,它会重复四次字段和值,有时甚至是 7 次。我不知道编辑操作是如何工作的,它使用与新操作相同的表单部分。但这是我对新操作和编辑操作的控制器操作。
此外,并非每个字段都发生重复,它似乎只发生在有has_many
关系的部分。
# GET /references/new
# GET /references/new.json
def new
@reference = Reference.new
@user_id = params[:user_id]
@reference.build_customer_overview
@reference.build_solution_description
@reference.solution_description.deal_details.build
component = @reference.components.build
component.servers.build
component.services.build
partner = @reference.build_partner
partner.competitors.build
partner.contacts.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @reference }
end
end
# GET /references/1/edit
def edit
@reference = Reference.find(params[:id])
@user_id = @reference.user_id
end
我只是不知道为什么它会在编辑操作中生成额外的字段,希望有更多 Rails 经验的人知道这种行为。
谢谢!
编辑:
这是表单的一部分,其中每个 ruby 块渲染 4 次。
<%= f.fields_for :deal_details do |ff| %>
<div>
<%= ff.label :title, 'Customer\'s business/IT needs (why was this solution required)' %>
<%= ff.hidden_field :title, value: 'business/IT needs' %>
<br />
<%= ff.text_area :content %>
</div>
<% end %>
<%= f.fields_for :deal_details do |ff| %>
<div>
<%= ff.label :title, 'Description of solution: (including product/services information)' %>
<%= ff.hidden_field :title, value: 'Description of solution' %>
<br />
<%= ff.text_area :content %>
</div>
<% end %>
<%= f.fields_for :deal_details do |ff| %>
<div>
<%= ff.label :title, 'Measurable anticipated benefits to the client and/or it\'s end customers: (i.e. this does NOT mean "benefits to")' %>
<%= ff.hidden_field :title, value: 'Measurable benefits' %>
<br />
<%= ff.text_area :content %>
</div>
<% end %>
<%= f.fields_for :deal_details do |ff| %>
<div>
<%= ff.label :title, 'Why won? (and why any competitors failed)' %>
<%= ff.hidden_field :title, value: 'Why won?' %>
<br />
<%= ff.text_area :content %>
</div>
<% end %>
最后更新:
好的,所以我需要制作一个单独的表格进行编辑,使用一个通用块循环通过数据库。这就是我所拥有的组件
<fieldset>
<% title = f.object.component_title %>
<h3><%= f.object.component_title %></h3>
<%= f.fields_for :servers do |fff| %>
<% if title == 'psg' %>
<%= render 'psg_fields', f: fff %>
<% end %>
<% if title == 'servers' %>
<%= render 'server_fields', f: fff %>
<% end %>
<% if title == 'storage' %>
<%= render 'storage_fields', f: fff %>
<% end %>
<% if title == 'ipg' %>
<%= render 'ipg_fields', f: fff %>
<% end %>
<% if title == 'software' %>
<%= render 'software_fields', f: fff %>
<% end %>
<% if title == 'services' %>
<%= render 'service_fields', f: fff %>
<% end %>
<% if title == 'financial' %>
<%= render 'financial_fields', f: fff %>
<% end %>
<% end %>
<%= link_to_add_fields "Add Server", f, :servers, :servers %>
</fieldset>