CSV import - with confirmation - standard solution

2019-08-28 17:59发布

问题:

In my application I wish to allow the user to upload a CSV file and then be presented with a view of their data mapped to my columns so that the user can confirm their data is correct. Ideally allowing them to edit incorrect data.

Are there any existing solutions to this via a gem, any other standard solution or any resources that might help with what I want to achieve.

Help very much appreciated.

回答1:

you can do something like:

require 'csv'

file_content = File.read(params[:file].tempfile.path)
csv = CSV.parse(file_content, :headers => true)
File.unlink(params[:file].tempfile.path)

depends on your params passed to the controller, but CVS can parse a file which is usually written to a tmp dir if uploaded, presentation of the result is up to your view layer