why 'method missing' error in ROR?
Despite the other excellent answers here to similar posts, I cannot see the error in my code that is producing this error: "undefined method `parse_file' for..." I'm using RoR (Rails 2.1) and am simply trying to upload and parse a csv file using FasterCSV plugin. I am also saving a record of the file's filename in the Upload table.
Many thanks for any help (this is driving me nuts...):
my model:
require 'fastercsv'
def new
@upload = Upload.new
end
def self.parse_file(file)
FasterCSV.foreach(file.path,:headers=>"first_row", :col_sep=>"\t") do |row|
row.each{|row| puts "row: #{row.inspect}"}
end
end
my controller:
def create
@up开发者_开发问答load = Upload.new
thefile = params[:upload][:upload_file]
@upload.filename = base_part_of(thefile.original_filename)
@upload.parse_file(thefile)
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 base_part_of(file_name)
File.basename(file_name)
end
my view:
<% form_for(:upload,
:url => {:action=> :create},
:html => { :multipart => true} ) do |form| %>
Upload your file: <%= form.file_field("upload_file",:size=>50,:class => "csv-input") %><br/>
<%= submit_tag("Upload") %>
<% end %>
the (partial) stack trace of the error:
vendor/rails/activerecord/lib/active_record/attribute_methods.rb:256:in `method_missing'
vendor/rails/actionpack/lib/action_controller/base.rb:1162:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:1162:in `perform_action_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:580:in `call_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:573:in `perform_action_without_benchmark'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
vendor/rails/actionpack/lib/action_controller/rescue.rb:201:in `perform_action_without_caching'
vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'
vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
vendor/rails/actionpack/lib/action_controller/base.rb:529:in `send'
vendor/rails/actionpack/lib/action_controller/base.rb:529:in `process_without_filters'
vendor/rails/actionpack/lib/action_controller/filters.rb:569:in `process_without_session_management_support'
vendor/rails/actionpack/lib/action_controller/session_management.rb:130:in `process'
vendor/rails/actionpack/lib/action_controller/base.rb:389:in `process'
...
You are calling a class method (Upload.parse_file) on an instance (@upload). Either change your definition:
class Upload
def parse_file(file)
FasterCSV.foreach(file.path,:headers=>"first_row", :col_sep=>"\t") do |row|
row.each{|row| puts "row: #{row.inspect}"}
end
end
end
or change your call:
Upload.parse_file(thefile)
精彩评论