[英]simple_fields_for was remove once the validation occur
I need some help.我需要一些帮助。
"= form.association:lead"
: the user can select a lead through select2 via ajax. "= form.association:lead"
:用户可以通过 ajax 通过 select2 引导 select。
.lead-form-inputs
: the user can create a lead and automatically set the lead_id of the property once it was submitted. .lead-form-inputs
:用户可以创建潜在客户并在提交后自动设置属性的lead_id。
I do have a select2 gem installed and runs the "= form.association:lead"
as a select2.我确实安装了 select2 gem 并将"= form.association:lead"
作为 select2 运行。 As you can see there was a:new_lead input as a boolean(a Checkbox).如您所见,有一个:new_lead 输入作为布尔值(一个复选框)。 Once the checkbox is checked the "= form.association:lead"
will disappear and the .lead-form-inputs
class will appear.选中复选框后, "= form.association:lead"
将消失, .lead-form-inputs
class 将出现。
PROBLEM:问题:
= form.simple_fields_for:lead do | lead_builder |
= form.simple_fields_for:lead do | lead_builder |
= form.simple_fields_for:lead do | lead_builder |
entire inputs is removed.整个输入被删除。I need that
form.simple_fields_for
to stay because if the user want to create another lead yet the validations has been triggered theform.simple_fields_for
is not present.我需要form.simple_fields_for
保留,因为如果用户想要创建另一个潜在客户但验证已被触发,则form.simple_fields_for
不存在。 Yet still the user can go to the property/new just to refresh the entire page.然而,用户仍然可以 go 到属性/新只是为了刷新整个页面。
"= form.association:lead"
and submitted the form(on purpose) just to trigger the validation.如果我 select 使用"= form.association:lead"
并提交表单(故意)只是为了触发验证。 The form.simple_fields_for
stays but "= form.association:lead"
is empty. form.simple_fields_for
保留,但"= form.association:lead"
为空。 I said earlier that "i select a lead".我之前说过“我是 select 一个潜在客户”。 Once i checked the checkbox.一旦我检查了复选框。 The form.simple_fields_for
inputs was filled in. The information filled in to the inputs is that the lead i selected earlier after i submitted the form.填写了form.simple_fields_for
输入。输入的信息是我在提交表单后之前选择的潜在客户。What i want is that the select2 will still display the lead what i selected.我想要的是 select2 仍将显示我选择的线索。
VIEW:看法:
= 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;
})
};
}
}
});
Property CONTROLLER:属性 CONTROLLER:
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
MODELS:楷模:
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
PROBLEM ANSWER:问题解答:
after_validation :leader_builder
def leader_builder
self.build_lead unless lead
end
= form.association :lead, collection: Lead.order("id ASC").map{|r| [r.full_name, r.id]}, input_html: { multiple: false }, label: false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.