1

I am building a form that allows a tournament director to select which fields he will use, for example first_name, last_name, dob, gender. After that seciton I want to allow custom fields like gi_size. It works gret, but when I go back to the edit form it shows the field I entered, but if I submit the form it adds that field again. It not only shows the one added, it shows an emplty one and when I update it submits them both.

I put the check box to allow them to not use it if they decide not to but it just adds it again.

Any thoughts on a links to add/remove the field so at least the new custom field won't be added when left empty.

Here is the form code:

<h4>Athlete Information</h4>
<%= form_with(model: @event, class: "shadow p-3 mb-3 rounded text-dark", local: true) do |f| %>
  <%= f.hidden_field :athlete_info_complete, value: 1 %>
  <%= f.hidden_field :next, value: 6 %>
  <div class="row club_container">
    <div class="col-12">
      <legend>
        Check all the boxes next to the information you would like to
        request from your tournament participants.  Each area will have an option 
        to add custom fields.
      </legend>
      <br><br>
      <h4>Basic Information</h4>
      <div class="col-md-9 col-sm-12">
        <label class="main">First Name
          <%= f.check_box :first_name %>
          <span class="w3docs"></span>
        </label>
        <label class="main">Last Name
          <%= f.check_box :last_name %>
          <span class="w3docs"></span>
        </label>
        <label class="main">Date of birth
          <%= f.check_box :dob %>
          <span class="w3docs"></span>
        </label>

        <h4>Custom Fields</h4><br>
        <small>Enter the name of your custom field. Please do 
        not use spaces or special characters except "_" for example "gi_size" rather than
        "gi size".
        <br>
        Also, make sure the checkbox next to the custom field box is checked if you wish to use the field.
        You can come back at anytime and uncheck the box if you decide not to use the field.
        <%= f.fields_for :custfields do |custfield_builder| %>
          <div class="form-group row">
            <div class="col-md-3 col-sm-12 col-form-label">
              <%= custfield_builder.label :custom_field_name %>
            </div>
            <div class="col-md-8 col-sm-12">
              <%= custfield_builder.text_field :custfield_name, class: 'form-control', placeholder: 'Custom_field_name' %>
              <%= custfield_builder.hidden_field :custfield_type, value: "basic" %>
            </div>
            <div class="col-md-1 col-sm-12">
              <label class="main">
                <%= custfield_builder.check_box :status %>
                <span class="w3docs"></span>
              </label>
            </div>
          </div>
        <% end %>
      
  

  <div class="form-group row">
    <div class="col-md-9 col-sm-12"></div>
    <div class="col-md-3 col-sm-12">
      <%= f.submit class: "profile_btn" %>
    </div>
  </div>
</div>
<% end %>

My event model:

class Event < ApplicationRecord
  has_many :custfields
  
  def custfields_attributes=(custfields_attributes)
    custfields_attributes.each do |i, custfield_attributes|
      self.custfields.build(custfield_attributes)
    end
  end
end

1 Answer 1

1

You need to add a hidden field named _destroy to the form for each nested model. Default the value to 0 (which is "don't destroy") and then change it to 1 when they click the checkbox.

Then you need to make sure you accept id and _destroy in the params in the controller.

And you need this in your model:

# app/models/event.rb:
has_many :custfields, allow_destroy: true

The Cocoon gem abstracts most of this for you. And GoRails has a video on how to build it by hand using StimulusJS.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I didn't respond sooner but the Gorails video was exatly what I was looking for. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.