Create csv from csv content
This commit is contained in:
@@ -43,6 +43,18 @@ class ItemsController < ApplicationController
|
|||||||
["Import items", "import_items"],
|
["Import items", "import_items"],
|
||||||
["Update prices", "update_prices"] ]
|
["Update prices", "update_prices"] ]
|
||||||
|
|
||||||
|
@csv_content = params[:csv_content]
|
||||||
|
|
||||||
|
@error_message = ""
|
||||||
|
@error_message = "Format of CSV is wrong (CSV content is empty)" if params[:csv_content] == ""
|
||||||
|
|
||||||
|
csv_file = ItemsHelper::create_csv(CSV.parse(params[:csv_content]))
|
||||||
|
begin
|
||||||
|
|
||||||
|
ensure
|
||||||
|
csv_file.unlink
|
||||||
|
end
|
||||||
|
|
||||||
render :template => "items/export_import"
|
render :template => "items/export_import"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
module ItemsHelper
|
module ItemsHelper
|
||||||
|
def self.create_csv(data)
|
||||||
|
file = Tempfile.new([Rails.root.join('tmp/').to_s, ".csv"], "")
|
||||||
|
csv = CSV.new(file)
|
||||||
|
data.each do |row|
|
||||||
|
csv << row
|
||||||
|
end
|
||||||
|
|
||||||
|
file.rewind
|
||||||
|
file.close
|
||||||
end
|
file
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user