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
On Feb 17, 2011, at 10:29 AM, rixter wrote:
> 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' %>
> <p>
> <%= submit_tag "Upload" %>
> </p>
>
> <% 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:in `method_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 received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.