Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have found solution and here are my tips. Hope it can be helpfull for someone faced with the same issue.</p> <p><strong>1) Route:</strong></p> <pre><code>resources :leads do collection { post :export } end </code></pre> <p><strong>2) Controller action:</strong></p> <pre><code> def export case params[:commit] when "To Excel" then render xlsx: 'export' when "To CSV" then send_data @leads.to_csv_modified(@lead_ids), filename: 'export.csv' else render action: "index" end end </code></pre> <p><strong>3) View:</strong></p> <pre><code> &lt;%= form_tag export_leads_path, method: :post do %&gt; &lt;%= text_field_tag :created_at_first %&gt; &lt;%= text_field_tag :created_at_last%&gt; &lt;% Lead.new.attributes.keys[0...Lead.new.attributes.keys.length-1].each_with_index do |field, index| %&gt; &lt;%= check_box_tag "lead_ids[]", field, checked = true %&gt; &lt;%= field %&gt; &lt;% end %&gt; &lt;%= submit_tag 'To Excel', controller: 'leads', action: "export" %&gt; &lt;%= submit_tag 'To CSV', controller: 'leads', action: "export" %&gt; &lt;% end %&gt; </code></pre> <p><strong>4) Axlsx template</strong> (invoking in controller with render xlsx: 'export'):</p> <pre><code>$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" require 'axlsx' p = Axlsx::Package.new wb = xlsx_package.workbook style_shout = wb.styles.add_style sz: 16, b: true, alignment: { horizontal: :center } wb.add_worksheet(name: "#{Time.now.to_date}") do |sheet| sheet.add_row params[:lead_ids] @leads.each_with_index do |lead, index| sheet.add_row params[:lead_ids].map {|e| lead.send(e) } end end </code></pre> <p><strong>5) Method to_csv_modified</strong> (invoking in controller with send_data @leads.to_csv_modified(@lead_ids), filename: 'import.csv'):</p> <pre><code>def self.to_csv_modified(lead_ids, options = {}) CSV.generate(options) do |csv| csv &lt;&lt; lead_ids all.each do |lead| csv &lt;&lt; lead.attributes.values_at(*lead_ids) end end end </code></pre> <p>If you would like to see how it is working, visit my github <a href="https://github.com/DavydenkovM/GetLead" rel="nofollow">conditional_exporting_to_XLSX_with_params</a></p> <p>Another useful links: <a href="https://github.com/randym/axlsx" rel="nofollow">Axlsx_documentation</a>, <a href="http://railscasts.com/episodes/396-importing-csv-and-excel" rel="nofollow">Railscast #396 - importing csv-and-excel</a>, <a href="http://railscasts.com/episodes/362-exporting-csv-and-excel" rel="nofollow">Railscast #362 - exporting csv-and-excel</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload