simple_fields_for 在验证发生后被删除

simple_fields_for was remove once the validation occur

我需要一些帮助。

"= form.association :lead" :用户可以通过 ajax.

select 通过 select2 引导

.lead-form-inputs :用户可以创建潜在客户并在提交后自动设置 属性 的 lead_id。

我确实安装了 select2 gem 并将 "= form.association :lead" 作为 select2 运行。 如您所见,有一个 :new_lead 输入作为布尔值(复选框)。 选中复选框后,"= form.association :lead" 将消失,.lead-form-inputs class 将出现。

问题:

  1. 一旦我(故意)提交表单只是为了触发验证。 = form.simple_fields_for :lead do | lead_builder | 整个输入被删除。

I need that form.simple_fields_for to stay because if the user want to create another lead yet the validations has been triggered the form.simple_fields_for is not present. Yet still the user can go to the property/new just to refresh the entire page.

  1. 如果我 select 一个使用 "= form.association :lead" 的潜在客户并(故意)提交表单只是为了触发验证。 form.simple_fields_for 保留,但 "= form.association :lead" 为空。 我之前说过“我 select 领先”。一旦我选中了复选框。 form.simple_fields_for 输入已填写。输入中填写的信息是我在提交表格后 select 早些时候编辑的潜在客户。

What i want is that the select2 will still display the lead what i selected.

查看:

= simple_form_for([:admin, @property], wrapper: :vertical_form, html: { autocomplete: "off", class: "primary-form" }) do |form|
  = form.input :listing_title
  = form.association :lead, collection: [], input_html: { multiple: false }, label: false

  = form.simple_fields_for :lead do | lead_builder |
    = lead_builder.input :new_lead, as: :boolean
    .lead-form-inputs.hide
     = lead_builder.input :status, collection: [['New Lead', 'New Lead'], ['SLC', 'SLC'], ['BLC', 'BLC'], ['Seller Lead', 'Seller Lead'], ['Buyer Lead', 'Buyer Lead'], ['Client', 'Client'], ['Shelf', 'Shelf'], ['Drop', 'Drop']], prompt: "Select Status"
     = lead_builder.input :last_name
     = lead_builder.input :first_name
     = lead_builder.input :middle_initial
     = lead_builder.input :contact_number
     = lead_builder.input :email_address
     = lead_builder.input :company_name
     = lead_builder.input :date_inquired do
     = lead_builder.text_field :date_inquired, class: "datepickerJs", value: @property.lead.date_inquired.blank? ? '' : @property.lead.date_inquired.strftime("%b-%d-%Y")
     = lead_builder.input :source, collection: [['Website', 'Website'], ['Facebook', 'Facebook'], ['Facebook Ads', 'Facebook Ads'], ['DotProperty', 'DotProperty'], ['Referral', 'Referral'], ['Network', 'Network'], ['Old Leads', 'Old Leads']], prompt: "Select Source"
  = form.submit "Save", class: "button action-button__save-button primary-form__submit-btn"

:javascript
  $( "#property_lead_id" ).select2({
    closeOnSelect: true,
    allowClear: true,
    placeholder: "Select lead",
    minimumInputLength: 1,
    ajax: {
      url: '/admin/leads.json',
      data: function (params) {
        var query = {
          search: params.term,
        }
        return query;
      },
      processResults: function (data) {
        return {
          results: $.map(data.leads, function(obj, key) {
            responsejson = {
                id: obj.id,
                text: `${obj.first_name} ${obj.middle_initial} ${obj.last_name}`,
            };
            return responsejson;
          })
        };

      }
    }
  });

属性 控制器:

def new
 @property = Property.new
 @lead = @property.build_lead
end

def create
 @property = Property.new(property_params)
 respond_to do |format|
  if @property.save
   format.html { redirect_to edit_admin_property_path(@property), notice: 'Property was successfully created.' }
  else
   format.html { render :new }
  end
 end
end

def property_params
  params.require(:property).permit(:listing_title, :lead_id, lead_attributes: [:id, :status, :last_name, :first_name, :middle_initial, :contact_number, :email_address, :company_name, :date_inquired, :source, :new_lead])
end

型号:

class Property < ApplicationRecord
 belongs_to :lead
 accepts_nested_attributes_for :lead, reject_if: :reject_lead

 def reject_lead(attributes)
    attributes["new_lead"] != '1'
  end
end

class Lead < ApplicationRecord
 attr_accessor :new_lead

 has_many :properties
 
 validates :status,
            :last_name,
            :first_name,
            :middle_initial,
            :contact_number,
            :email_address,
            :company_name,
            :date_inquired,
            :source,
            presence: true
end

问题答案:

  1. 我在 属性 模型中定义了一个新方法并使用回调 after_validation。
 after_validation :leader_builder

 def leader_builder
   self.build_lead unless lead
 end
  1. 我在 select2 的集合属性中添加了一个值。
= form.association :lead, collection: Lead.order("id ASC").map{|r| [r.full_name, r.id]}, input_html: { multiple: false }, label: false