ROR为什么'方法丢失'错误?

时间:2011-02-25 22:14:35

标签: ruby-on-rails

尽管这里有类似帖子的其他优秀答案,我看不到产生此错误的代码中的错误:“undefined method`parse_file'for ...” 我正在使用RoR(Rails 2.1),我只是尝试使用FasterCSV插件上传和解析csv文件。我还在上传表中保存文件文件名的记录。

非常感谢任何帮助(这让我疯了......):

我的模特:

   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

我的控制员:

 def create

    @upload = 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

我的观点:

<% 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 %>

错误的(部分)堆栈跟踪:

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'
...

1 个答案:

答案 0 :(得分:4)

您正在实例(@upload)上调用类方法(Upload.parse_file)。要么改变你的定义:

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

或更改您的电话:

Upload.parse_file(thefile)
相关问题