使用表单提交Ruby on rails文件下载

时间:2021-11-14 23:19:43

I have a page where I upload a list of users to an action upload data. There on success/failure it lists me the list of invalid users. This is a one time action the lsit of users is not saved to database. I need to be able to write these user list to a file and allow the user to download this file. I do not have a clue on how to proceed. Any help would be appreciated.

我有一个页面,我将用户列表上传到动作上传数据。在成功/失败时,它列出了无效用户列表。这是用户的lsit未保存到数据库的一次性操作。我需要能够将这些用户列表写入文件并允许用户下载此文件。我不知道如何继续。任何帮助,将不胜感激。

1 个解决方案

#1


3  

1. require the CSV library to controller

require 'csv'

2. Code in controller

    def export_to_csv
       #get all invalid users here and then modify according to yoou logic       
        @users = User.find(:all)
        csv_string = CSV.generate do |csv|
             csv << ["Id", "Name", "status","Role"]
             @users.each do |user|
               csv << [user.id, user.name, user.status, user.role]
             end
        end         

       send_data csv_string,
       :type => 'text/csv; charset=iso-8859-1; header=present',
       :disposition => "attachment; filename=users.csv" 
    end 

#1


3  

1. require the CSV library to controller

require 'csv'

2. Code in controller

    def export_to_csv
       #get all invalid users here and then modify according to yoou logic       
        @users = User.find(:all)
        csv_string = CSV.generate do |csv|
             csv << ["Id", "Name", "status","Role"]
             @users.each do |user|
               csv << [user.id, user.name, user.status, user.role]
             end
        end         

       send_data csv_string,
       :type => 'text/csv; charset=iso-8859-1; header=present',
       :disposition => "attachment; filename=users.csv" 
    end