What's the correct syntax to process a file_field object?

I am using Rails 2.1 (and RUBYGEMS VERSION: 1.5.0 RUBY VERSION: 1.8.7)
and
have been having a devil of a time trying to get a simple thing to
work:
upload and process a CSV file.
After extensively researching many examples both here and elsewhere, I
have
not been successful in getting this work; yet, I feel I’m so close!
So, I am in hopes that someone here could please point out my
(hopefully
slight) errors below; my current bug is in the first lines of the
controller.

Once this part is working, I’ll finish the code that parses the file and
stores the data into the database. I do not need to store the uploaded
file
itself, though I am storing the name of the file that gets uploaded (in
the
Upload table). I also realize it is better style to have ‘fat models and
thin controllers’ but I’m just trying to get this work.

Thanks in advance to anyone who can help!

the view code:
<% form_for(:upload,
:url => {:action=> :create},
:html => { :multipart => true} ) do |form| %>

Upload your file: <%= form.file_field(“upload_file”,:size=>50,:class
=>
“csv-input”) %>

<%= submit_tag(“Upload”) %>

<% end %>

the controller code:
def create
@upload = Upload.new(params[:upload][:upload_file])
thefile = File.new(params[:upload][:upload_file])
@upload.filename = base_part_of(thefile.original_filename)
@upload.assign_name(thisfilename)
@upload.call(parse_file(params[:upload_file]))

respond_to do |format|
  if @upload.save
    flash[:notice] = 'Upload was successful.'
    format.html { redirect_to(@upload) }
    format.xml  { render :xml => @upload, :status => :created, 

:location
=> @upload }
else
format.html { render :action => “new” }
format.xml { render :xml => @upload.errors, :status =>
:unprocessable_entity }
end
end
end

def parse_file(file)
FasterCSV.foreach(file.path,:headers=>“first_row”, :col_sep=>"\t")
do
|row|
row.each{|row| puts “row: #{row.inspect}”}
end

the model code:
def new
@upload = Upload.new
end

def assign_name(n)
@upload.filename = n
end

On Fri, Feb 25, 2011 at 2:27 PM, rixter [email protected] wrote:

the controller code:
def create
@upload = Upload.new(params[:upload][:upload_file])
thefile = File.new(params[:upload][:upload_file])
@upload.filename = base_part_of(thefile.original_filename)
@upload.assign_name(thisfilename)
@upload.call(parse_file(params[:upload_file]))

The last line above is calling params[:upload_file] and not
params[:upload][:upload_file]. params[:upload_file] doesn’t exist
according
to your view layout.

B.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs