0

我有一个注册表单,允许锦标赛主管选择他们在参与者注册锦标赛时将要求的字段。我按照 Go Rails 上的视频使用刺激 JS 将动态嵌套表单字段添加到表单中,效果很好。

我正在尝试在表单的另一部分中添加第二组字段。该表格分为部分,基本信息,联系信息,比赛信息等,我希望能够向每个部分添加自定义字段。

在基本部分它工作得很好。我可以添加和删除自定义字段,但是当我将它添加到联系人部分时,即使将嵌套表单更改为嵌套联系人,它也只会在我在基本部分中创建的字段中添加。我可以从任一部分添加和删除,但它只是在两个部分中都显示它们。

这是我现在的表格。我仍然需要添加其他部分,但在添加表单的其余部分之前,我试图让基本和联系人自定义字段正常工作:

<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="form-group row">
        <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>
        </div>
      </div>
      <h4>Custom Fields</h4>
      <small>
        Enter the name of your custom field you plan to offer.
      </small>
      <br><br>
      
      <div data-controller="nested-form">
        <template data-target="nested-form.template">
          <%= f.fields_for :fields, Field.new, child_index: 'NEW_RECORD' do |field| %>
            <%= render 'events/forms/custom_fields_basic', f: field %>
          <% end %>
        </template>
        
        <%= f.fields_for :fields do |field| %>
          <%= render 'events/forms/custom_fields_basic', f: field %>
        <% end %>
        <div class="mb-3" data-target="nested-form.links">
          <%= link_to 'Add Custom Field', "#", class: "btn btn-outline-dark", data: { action: "click->nested-form#add_association" } %>
        </div>
      </div>

      <h4>Contact Information</h4>
      <div class="form-group row">
        <div class="col-md-9 col-sm-12">
          <label class="main">Address 1
            <%= f.check_box :address1 %>
            <span class="w3docs"></span>
          </label>
          <label class="main">Address 2
            <%= f.check_box :address2 %>
            <span class="w3docs"></span>
          </label>
          <label class="main">City
            <%= f.check_box :city %>
            <span class="w3docs"></span>
          </label>
        </div>
      </div>

      <div data-controller="nested-contacts">
        <template data-target="nested-contacts.template">
          <%= f.fields_for :fields, Field.new, child_index: 'NEW_RECORD' do |field| %>
            <%= render 'events/forms/custom_fields_contact', f: field %>
          <% end %>
        </template>
        
        <%= f.fields_for :fields do |field| %>
          <%= render 'events/forms/custom_fields_contact', f: field %>
        <% end %>
        <div class="mb-3" data-target="nested-contacts.links">
          <%= link_to 'Add Custom Field', "#", class: "btn btn-outline-dark", data: { action: "click->nested-contacts#add_association" } %>
        </div>
      </div>
      
      <div class="form-group row">
        <div class="col-md-9 col-sm-12"></div>
        <div class="col-md-3 col-sm-12">
          <button id="button_next" class="profile_btn align-middle" style="width:180px;">Finalize <span style="margin-top: 5px; font-size: 18px;"><i class="fas fa-long-arrow-alt-right"></i></span></button>
        </div>
      </div>
    </div>
  </div>
<% end %>

这是 _custom_fields_basic.html.erb

<%= content_tag :div, class: 'nested-fields', data: { new_record: f.object.new_record? }  do %>
  <div class="form-group row">
    <div class="col-md-3 col-sm-12 col-form-label">
      <%= f.label :field_name %>
    </div>
    <div class="col-md-9 col-sm-12">
      <%= f.text_field :name, class: 'form-control' %>
      <small><%= link_to 'Remove', '#', data: { action: 'click->nested-form#remove_association' } %></small>
    </div>
  </div>
  <%= f.hidden_field :field_type, value: 'basic' %>
  <%= f.hidden_field :_destroy %>
<% end %>

这是 custom_fields_contact.html.erb 文件:

<%= content_tag :div, class: 'nested-contacts', data: { new_record: f.object.new_record? }  do %>
  <div class="form-group row">
    <div class="col-md-3 col-sm-12 col-form-label">
      <%= f.label :field_name %>
    </div>
    <div class="col-md-9 col-sm-12">
      <%= f.text_field :name, class: 'form-control' %>
      <small><%= link_to 'Remove', '#', data: { action: 'click->nested-contacts#remove_association' } %></small>
    </div>
  </div>
  <%= f.hidden_field :field_type, value: 'contact' %>
  <%= f.hidden_field :_destroy %>
<% end %>

这是 nexted_form_controller.js

import { Controller } from "stimulus"

export default class extends Controller {
  static targets = [ "links", "template" ]

  connect() {
    
  }

  add_association(event) {
    event.preventDefault()

    var content = this.templateTarget.innerHTML.replace(/NEW_RECORD/g, new Date().getTime())
    this.linksTarget.insertAdjacentHTML('beforebegin', content)
  }
  
  remove_association(event) {
    event.preventDefault()

    let wrapper = event.target.closest(".nested-fields")
    if (wrapper.dataset.newRecord == "true") {
      wrapper.remove()
    } else {
      wrapper.querySelector("input[name*='_destroy']").value = 1
      wrapper.style.display = 'none'
    } 
  }
}

还有nested_contacts_controller.js:

import { Controller } from "stimulus"

export default class extends Controller {
  static targets = [ "links", "template" ]

  connect() {
    
  }

  add_association(event) {
    event.preventDefault()

    var content = this.templateTarget.innerHTML.replace(/NEW_RECORD/g, new Date().getTime())
    this.linksTarget.insertAdjacentHTML('beforebegin', content)
  }
  
  remove_association(event) {
    event.preventDefault()

    let wrapper = event.target.closest(".nested-contacts")
    if (wrapper.dataset.newRecord == "true") {
      wrapper.remove()
    } else {
      wrapper.querySelector("input[name*='_destroy']").value = 1
      wrapper.style.display = 'none'
    } 
  }
}

如果有办法用一个控制器来做这件事会很棒,但如果有必要,我可以为每个部分设置单独的控制器。

谢谢

4

1 回答 1

0

您的问题不在于刺激控制器,而在于您如何定义这些字段。不过,需要注意的一点是,Stimulus 是模块化的,因此您只需要一个控制器,并且可以在同一页面上实现任意多次。的目的data-controller是定义控制器可以影响 DOM 的哪一部分。

# form
<div data-controller="nested-form">
  Affects only things in here
</div>

<div data-controller="nested-form">
  Affects only things in here
</div>

问题是您在两个地方使用相同的关联模型。呈现编辑页面时,表单将显示每个部分中的所有字段,因为您要求它使用这些行

<%= f.fields_for :fields do |field| %> #specifically this one
  <%= render 'events/forms/custom_fields_basic', f: field %>
<% end %>

这将调用与事件关联的所有关联模型并显示它们的字段。

你有隐藏字段,field_type所以现在你只需要在事件模型中定义一些方法。

# model
def basic_fields
  self.fields.where('field_type = ?', 'basic')
end

现在,而不是<%= f.fields_for :fields do |field| %>,然后你做<%= f.fields_for :basic_fields do |field| %>等等。

于 2021-02-05T20:16:34.247 回答