'rails generated CSV file can't be downloaded
I'm trying to export my database to CSV file and download it but it doesn't download. I have a form with 2 different actions, Export to CSV and Export to JSON I want to when I click any submit button of them to download the generated file but it doesn't work. When Enter the city name and click the Export to CSV submit button it doesn't download
the view export.html.erb
<h1>Export Data</h1>
<div class="container">
<div class="row">
<div class="col-xs-12">
<%= form_with(url: export_path, method: 'get', local: true) do |f| %>
<div class="form-group">
<%= f.label :City, style: "font-weight: bolder; font-size: large;" %>
<%= f.text_field :city, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :Date, style: "font-weight: bolder; font-size: large;" %>
<%= f.datetime_local_field :date, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Export to CSV", class: "btn btn-primary" %> |
<%= f.submit "Export to JSON", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
Solution 1:[1]
Okay, guys I finally solved the issue. Actually, it was more than one reason that caused it.
First I replaced this line:
<%= form_with(url: export_path, method: 'get', local: true) do |f| %>
with this line:
<%= form_with(url: export_path(format: "csv") , method: 'get', local: true) do |f| %>
thanks to my friend Jamie Crone
Solution 2:[2]
replace this line
<%= form_with(url: export_path, method: 'get', local: true) do |f| %>
with...
<%= form_with(url: export_path, method: 'get', local: true, format: :csv) do |f| %>
Solution 3:[3]
does anything download if you put this in the browser http://localhost:3000/export_path.pdf
Try changing this
<%= form_with(url: export_path, method: 'get', local: true) do |f| %>
with this
<%= form_with(url: export_path(format: "csv") , method: 'get', local: true) do |f| %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Eyeslandic |
Solution 3 | Jamie Crone |