Today i have a little problem containing a File Upload.
First some Infos:
Rubyversion: 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] Railsversion: Rails 3.1.1
Needed Code: My Form
<%= form_for @label, :html => { :multipart => true } do |f| %>
<% if @label.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@label.errors.count, "error") %> prohibited this label from being saved:</h2>
<ul>
<% @label.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :file %><br />
<%= f.file_field :file %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My Model:
class Label < ActiveRecord::Base
attr_accessor :file
attr_reader :file
def file=(file)
log = Logger.new(Rails.root.join("log/label.log").to_s)
log.info "file-Action called"
path=file.tempfile.to_path.to_s
filename="labellist.csv"
dest=Rails.root.join("app/assets/csv/").join(filename).to_s
FileUtils.cp(path, dest)
csv=CsvLabelParser.new
log.info "New Parser initialized!"
csv.parse_csv(dest)
log.info "Variables: path: #{path.inspect} <-----> dest: #{dest.inspect}"
end
end
My Parser
class CsvLabelParser
require 'csv'
def initialize
@logger=Logger.new(Rails.root.join("log/parser.log").to_s)
end
def logger
@logger
end
def parse_csv(path)
counter = 0
read_handle = File.open(path, "rb")
content = read_handle.read
self.logger.info "CONTENT: #{content.inspect}"
read_handle.close
content.each_line do |line|
if counter != 0
csv_array=line.split(";")
self.logger.info "CSV-Array No. #{counter}: #{csv_array.inspect}"
label=Label.new
label.labnr=csv_array[10]
label.name=csv_array[0]
label.firm1=csv_array[3]
label.firm2=csv_array[4]
label.postal=csv_array[7]
label.city=csv_array[8]
label.country=csv_array[9]
label.street=csv_array[5]
label.pob=csv_array[6]
label.save
end
counter += 1
end
end
end
Controller-Log
# Logfile created on 2012-06-06 09:49:23 +0200 by logger.rb/31641
Controller-create-method called! => variables: Params: {"utf8"=>"✓", "authenticity_token"=>"L+fpIMqFA9qe9U/LxU+atFONT8e3L5xEUum1321mRng=", "label"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x00000003df2318 @original_filename="ADRBW076-Q.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"label[file]\"; filename=\"ADRBW076-Q.csv\"\r\nContent-Type: text/csv\r\n", @tempfile=#<File:/tmp/RackMultipart20120606-2741-1izzofa>>}, "commit"=>"Create Label", "action"=>"create", "controller"=>"labels"}<-----> Label: #<Label id: nil, labnr: nil, name: nil, firm1: nil, firm2: nil, postal: nil, city: nil, country: nil, street: nil, pob: nil, created_at: nil, updated_at: nil>
So! As you can see i want to upload a CSV-file to parse it into my database. A copy of the File should be stored in a folder called CSV for later use. I've done that about 5 times before and it worked well, but this time it seems, that the file-method in the model isn't called. For now i only want it to work nothing more there will be some changes to catch errors and stuff like that in the future ;). I've logged the Controller action, which is called every time. May the Logfile can Help you! For me it looks okay, comparing it to other logfiles. I've searched for a solution, but everything looks okay to me (maybe i've just forgotten a : or a ,), so i can't really say what's wrong!
Thanks for your help