I need some help.
"= form.association :lead"
: the user can select a lead through select2 via ajax.
.lead-form-inputs
: the user can create a lead and automatically set the lead_id of the property once it was submitted.
I do have a select2 gem installed and runs the"= form.association :lead"
as a select2.
As you can see there was a :new_lead input as a boolean(a Checkbox).
Once the checkbox is checked the "= form.association :lead"
will disappear and the .lead-form-inputs
class will appear.
PROBLEM:
= 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. Yet still the user can go to the property/new just to refresh the entire page.
"= form.association :lead"
and submitted the form(on purpose) just to trigger the validation. The form.simple_fields_for
stays but "= form.association :lead"
is empty.
I said earlier that "i select a lead". 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.What i want is that the select2 will still display the lead what i selected.
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:
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