I am writing query output to a CSV file in a folder called ‘reports_output’ inside my ‘report_runs’ views folder. The file names need to be generated based upon the name given by users running reports and other parameters like current time etc. I have two issues:
- How to generate unique file names using Time.now converted to string of numbers (like version numbers in db/migrate) ? and
- How to do correct routing to all these .csv files?
I was able to write files with simple names e.g. Item_List.csv below, but not been able to view the files by clicking on the url to the file in my show view in browser. The error says :
No route matches "/report_runs/report_outputs/Item_List.csv"
1: You can generate a filename that includes the current time using simple string interpolation and you can format the string with Time#strftime:
2: If you want to serve static assets like your .csv files then you can write them to the public folder. So for example if you create a file in
public/report_runs/report_outputs/Item_List.csvit will be accessible from the path given in your example.