Hold both params of Decendant checkbox accociate w

2019-08-24 06:51发布

问题:

There is a select_tag to select the customer. When user selected a customer, it will generate a child checkbox_from below the select_tag.

User can select a customer and check the decendant checkbox both or either only customer.

If user select only a customer then searched,It will get the results and select_tag value(param) also hold in the select tag after the refresh.

But the problem is if I try get the both select_tag and checkbox_tag data, it will generate the data but both params are gone. I need to hold both params after the search results.

This is my view

.row
            .col-md-3
              = label_tag "Customer/Supplier Name"
              = select_tag "search_customer_supplier[id]", options_from_collection_for_select(Organization.customers_and_suppliers, :id, :name, params.dig('search_customer_supplier', 'id')), class: "form-control parent_class chosen-select", id: "search_registered_customers", include_blank: true
              .col-md-10.small_scale_margin-top2#check_children_wrapper.hide
                = label_tag "Descendent"
                = check_box_tag "organization_children", "true", params.dig('search_customer_supplier', 'organization_children')&.include?('true')

This is my controller code regarding to the checkbox

def customer_supplier_report
    Organization
    Address
    ContactNumber
    refined_query = ""
    if params[:search].present? or params[:excel_report].present?

      search_customer_supplier = params[:search_customer_supplier]
      if params[:organization_children].present? and search_customer_supplier["id"].present?
        organization_id = search_customer_supplier["id"]
        organization = Organization.find(organization_id)
        anchestor_ids = organization.anchestors.map{|o| o[:member].id }
        search_customer_supplier["id"] = "(#{anchestor_ids.join(' OR ')})" if anchestor_ids.any?
        end

      params[:search_customer_supplier]['accounts_dealer_types.dealer_code'] = params[:search_customer_supplier]['accounts_dealer_types.dealer_code'].join(" OR ") if params[:search_customer_supplier]['accounts_dealer_types.dealer_code'].present?

      customer_report = params[:search_customer_supplier].map { |k, v| "#{k}:#{v}" if v.present? }.compact

回答1:

This is the opposite of your previous question.

Your check_box_tag's parameter name is 'organization_children', so it will be passed in params[:organization_children]. You access it correctly in controller, do the same in the view:

= check_box_tag "organization_children", "true", params[:organization_children].present?