Dipak Panchal
Dipak Panchal

Reputation: 6026

Export to CSV File

http://railscasts.com/episodes/362-exporting-csv-and-excel

I'm following that railcast episode. It's working perfect but while I export to csv file that unable add .csv extension means that it's store like products not as products.csv.

I want to store like products.csv how can do that means how can i add extension during saving that file that take automatically add extension during save csv file.

Update

enter image description here

When I export CSV file then I will open popup like above image. but I want products.csv in the Name:

Update 2

Controller

  respond_to do |format|
    format.html
    format.csv { send_data @customers.to_csv }
    format.xls
  end


View


<%= link_to "CSV", "/admin/partners?id=#{params[:id]}&start_date=#{params[:start_date]}&end_date=#{params[:end_date]}&format=csv" %> 

<%= link_to "Excel", "/admin/partners?id=#{params[:id]}&start_date=#{params[:start_date]}&end_date=#{params[:end_date]}&format=xls" %> 

Thanks

Upvotes: 0

Views: 948

Answers (2)

Mohamed El Mahallawy
Mohamed El Mahallawy

Reputation: 13842

Controller:

respond_to do |format|
        format.csv { send_data @report.to_csv, filename: @report.name + ".csv"} #to export csv
end

In the view

<%= link_to 'CSV', report_path(report, format: :csv)%>

Upvotes: 4

Raghvendra Parashar
Raghvendra Parashar

Reputation: 4053

try this, it should work.

In view


<%= link_to "Products CSV", products_path(:format => 'csv') %>

In Controller

def index
  products = Product.all
  respond_to do |format|
    format.csv do
      send_data products.to_csv
    end
  end
end

Upvotes: 2

Related Questions