23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/csv_mapper/controller_actions.rb', line 23
def import
resource_name = self.class.name.gsub(/Controller/, '').singularize
resource_class = resource_name.constantize
if request.post?
if params[:fields]
create_resource_items_from_csv(resource_class)
if @csv_import_errors.empty?
flash[:notice] = 'Daten erfolgreich importiert!'
redirect_to :action => :index
else
flash[:warning] = @csv_import_errors
redirect_to :back
end
else
@mapper = CsvMapper::Importer.new(params, self.class.read_inheritable_attribute(:map_fields_options))
@raw_data = @mapper.raw_data
render 'controller_actions/mapper'
end
else
render 'controller_actions/import'
end
rescue CsvMapper::InconsistentStateError
flash[:warning] = 'unbekannter Fehler.'
rescue CsvMapper::MissingFileContentsError
flash[:warning] = 'Bitte eine CSV-Datei hochladen.'
render 'controller_actions/import'
rescue FasterCSV::MalformedCSVError => e
flash[:warning] = 'Fehlerhaft formatierte CSV-Datei: ' + e
render 'controller_actions/import'
rescue Errno::ENOENT
flash[:warning] = 'Datei nicht mehr auf dem Server. Bitte erneut hochladen!'
render 'controller_actions/import'
end
|