guide
- form
<%= form_with(url: upload_table_records_path(@table), method: :POST, local: true) do |form| %>
<div class="form-group row justify-content-center">
<%= form.file_field :file, class: 'form-control col-6' %>
</div>
<div class="actions text-center">
<%= submit_tag t("upload"), class: "btn btn-primary"%>
</div>
<% end %>
- controller
f = params[:file]; contents = f.read;
fpath = f.path; # same with f.tempfile.path
File.open(fpath, 'wb') do |file|
file.write(contents)
file.close
end
Record.from_csv(@table,fpath)
- Note that params[:file] is
ActionDispatch::Http::UploadedFile
object
- It has tempfile stored in tmp files, e.g.
/tmp/RackMultipart20210118-18052-19hic1b.csv
, which will be deleted when the file is closed.