Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails fields_for generating too many fields on edit action
    primarykey
    data
    text
    <p>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.</p> <p>Also, the duplication doesn't happen for every field, it seems it only happens in sections where there is a <code>has_many</code> relationship.</p> <pre><code> # 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 </code></pre> <p>I just have no idea why it would generate extra fields on the edit action, hoping someone with more rails experience knows the behavior. </p> <p>Thanks!</p> <p>EDIT:</p> <p>Here is a section of the form where each ruby block renders 4 times.</p> <pre><code> &lt;%= f.fields_for :deal_details do |ff| %&gt; &lt;div&gt; &lt;%= ff.label :title, 'Customer\'s business/IT needs (why was this solution required)' %&gt; &lt;%= ff.hidden_field :title, value: 'business/IT needs' %&gt; &lt;br /&gt; &lt;%= ff.text_area :content %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;%= f.fields_for :deal_details do |ff| %&gt; &lt;div&gt; &lt;%= ff.label :title, 'Description of solution: (including product/services information)' %&gt; &lt;%= ff.hidden_field :title, value: 'Description of solution' %&gt; &lt;br /&gt; &lt;%= ff.text_area :content %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;%= f.fields_for :deal_details do |ff| %&gt; &lt;div&gt; &lt;%= ff.label :title, 'Measurable anticipated benefits to the client and/or it\'s end customers: (i.e. this does NOT mean "benefits to")' %&gt; &lt;%= ff.hidden_field :title, value: 'Measurable benefits' %&gt; &lt;br /&gt; &lt;%= ff.text_area :content %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;%= f.fields_for :deal_details do |ff| %&gt; &lt;div&gt; &lt;%= ff.label :title, 'Why won? (and why any competitors failed)' %&gt; &lt;%= ff.hidden_field :title, value: 'Why won?' %&gt; &lt;br /&gt; &lt;%= ff.text_area :content %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>FINAL UPDATE:</p> <p>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</p> <pre><code> &lt;fieldset&gt; &lt;% title = f.object.component_title %&gt; &lt;h3&gt;&lt;%= f.object.component_title %&gt;&lt;/h3&gt; &lt;%= f.fields_for :servers do |fff| %&gt; &lt;% if title == 'psg' %&gt; &lt;%= render 'psg_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'servers' %&gt; &lt;%= render 'server_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'storage' %&gt; &lt;%= render 'storage_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'ipg' %&gt; &lt;%= render 'ipg_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'software' %&gt; &lt;%= render 'software_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'services' %&gt; &lt;%= render 'service_fields', f: fff %&gt; &lt;% end %&gt; &lt;% if title == 'financial' %&gt; &lt;%= render 'financial_fields', f: fff %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= link_to_add_fields "Add Server", f, :servers, :servers %&gt; &lt;/fieldset&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload