Need help w/ MVC coding of simple use of FasterCSV

I’m new to ROR, so still getting used to the MVC syntax; but think I
have a
simple example here that I hope someone can tell me what I’m doing wrong
(&
maybe help someone else by doing so):

All I’m trying to do is upload a CSV file, then parse each row of data
in it
(the first how contains header data).

Here’s the essential part of my view:

<% form_tag ({:action => ‘process_file’},
:multipart => true) do %>
<%= file_field_tag ‘file’ %>

<%= submit_tag "Upload" %>

<% end %>

here’s my controller:

def process_file
post = Upload.import(params[:file])
flash[:ok] = “data imported”
redirect_to :action => index
end

and here’s my model, where I’m not parsing anything yet, just putting a
stub
message to the screen (can one do that in a model? not sure…), just to
see
if this part works:

require ‘fastercsv’

def self.import(file)
datafile = FasterCSV.new(params[:file])
n = 0
FasterCSV.foreach(datafile) do |row|
puts 'on row '.n
n = n+1
end
end

However, the error I get when I submit a file is:

NameError in UploadsController#process_file

undefined local variable or method params' for #<Class:0xb6987e78> ... vendor/rails/activerecord/lib/active_record/base.rb:1667:inmethod_missing’
app/models/upload.rb:21:in `import’

Can anyone please help me to spot the error of my ways?

thanks in advance,
rick
app/controllers/uploads_controller.rb:90:in `process_file’

You’re referencing params from your model - you don’t have access to
that.
datafile = FasterCSV.new(params[:file])
should be
datafile = FasterCSV.new(file)

Best Wishes,
Peter

Thanks! That helped…(.now on to the next bug…)

rick

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