I need to export my Model to .xlsx and to submit some parameters (checkbox) for axlsx template.
I have a controller "leads" and custom post action "export", which should invoke my export.xlsx.axlsx template:
def export
respond_to do |format|
format.xlsx ## here is invoking my export.xlsx.axlsx
end
end
In my routes.rb I have the following:
resources :leads do
collection { post :ipmort }
end
I am trying to implement the following view:
<%= form_tag export_leads_path do %>
<%= button_to "Export to Excel", {controller: 'leads', action: "export",
remote: true, form: { "data-type" => "xlsx" }} %>
<% end %>
But when I click this button I have got html data format, but not xlsx
Is there any possibility to specify respond_to format in submit_tag, button_to?
I have tried to use <%= link_to %> but then I cannot get my form params..
<%= form_tag export_leads_path do %>
<%= link_to "Excel", export_leads_path(format: "xlsx", commit: "Excel"), method: :post %>
<% end %>
Here i haven't lead_ids params:
I also have tried <% submit_tag %>, but result was the same as <%= button_to %>
Please help me, I have spent all the day struggling with this issue and have no thoughts what can I do else.. Let me know if you need any additional information and I will provide it as soon as possible!
Thanks in advance!
I have found solution and here are my tips. Hope it can be helpfull for someone faced with the same issue.
1) Route:
2) Controller action:
3) View:
4) Axlsx template (invoking in controller with render xlsx: 'export'):
5) Method to_csv_modified (invoking in controller with send_data @leads.to_csv_modified(@lead_ids), filename: 'import.csv'):
If you would like to see how it is working, visit my github conditional_exporting_to_XLSX_with_params
Another useful links: Axlsx_documentation, Railscast #396 - importing csv-and-excel, Railscast #362 - exporting csv-and-excel