I have a really beast form that is essentially a five page sales form, and when creating a new form all is fine. But when you go to edit the new form, it duplicates fields and values four times, and sometimes 7 times. I have no idea how the edit action is even working, it uses the same form partial that the new action uses. But here is my controller action for the new and edit actions.
Also, the duplication doesn't happen for every field, it seems it only happens in sections where there is a has_many relationship.
# 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
I just have no idea why it would generate extra fields on the edit action, hoping someone with more rails experience knows the behavior.
Thanks!
EDIT:
Here is a section of the form where each ruby block renders 4 times.
<%= 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 %>
FINAL UPDATE:
Ok, so I needed to make a seperate form for editing that uses one generic block to loop through the DB. this is what I have for the components
<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>
It looks like you when you create a the parent object Reference, you are creating multiple deal details. By default, when the fields_for helper is used on a has_many relationship it will repeat the fields_for block once for every instance of the child (`deal_detail' in this case).
To only display the correct instance, simply add your instance to the fields_for like this, assuming you have a deal_detail instance saved to @my_deal_detail
<%= f.fields_for :deal_details, @my_deal_detail do |ff| %>
There is more information in the documentation for fields_for under 'has_many'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With