搜索后如何在Rails中保持复选框的真值

时间:2019-05-15 06:01:57

标签: ruby-on-rails ruby ruby-on-rails-4

搜索工作正常,但是问题是当我根据结果打印excel报告时,它显示数据库中的所有值,过滤器无法正常工作,然后Checkbox的真值消失了。刷新页面后如何保留“所有地址”复选框的参数。

这是我的观点

.col-md-3.small_scale_margin-top2
  = check_box_tag "all_address"
  = label_tag "Show All Addresses"

这是我的控制人

    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
      #puts "======================================================================"
      # puts params[:search_customer_supplier]['accounts_dealer_types.dealer_code']

      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?

      # puts params[:search_customer_supplier]['accounts_dealer_types.dealer_code']

      customer_report = params[:search_customer_supplier].map { |k, v| "#{k}:#{v}" if v.present? }.compact
    else
      customer_report = ["accounts_dealer_types.dealer_code:(CUS OR SUP OR INDCUS OR INDSUP)"]
    end

    @all_address = params[:all_address].to_bool if params[:all_address].present?
    refined_query += customer_report.join(" AND ")
    params[:query] = refined_query
    # params[:per_page] = 500
    @customer_reports = Organization.search(params)

    puts "========================================================="
      puts @customer_reports
    puts "========================================================="

    @account_managers = User.where(active: true)
    respond_to do |format|
      if params[:excel_report].present?
        request.format = "xls"
        format.xls { set_attachment_name "customer_supplier_report.xls" }
      else
        format.html
      end
    end

  end

在我的控制器中,这是复选框的相关部分

@all_address = params[:all_address].to_bool if params[:all_address].present?

1 个答案:

答案 0 :(得分:1)

只需使用@all_address来设置check_box_tagchecked属性

 = check_box_tag "all_address", "1", @all_address

注意:由于checked属性是列表中的第三个属性,因此您也必须提供第二个属性。 "1"只是无论如何将用于第二个参数的默认值。