2012-03-17 67 views
1

這是關於表單中的字段中,我使用csv文件解析和上傳給錯誤在ROR

<%= f.file_field :file ,:url=>{:controller=>"retailers",:action=>"csv_import"}%> 

以下是控制器代碼

def create 
    @retailer = Retailer.new(params[:retailer]) 
    respond_to do |format|  
      if verify_recaptcha(:model =>@retailer) && @retailer .save 

     # To notify newly registered user. 
      retailer_ids = [@retailer.id] 
      Emailer.on_notify_retailer(retailer_ids, 1, 0) 
     sign_in @retailer 
     format.html { redirect_to pages_about_path} 
     flash[:notice1] = "Thank you for registering with Chindi." 
     flash[:notice2] = "We will process your application and get back to you within 48 hours. Once approved, you will be able to create negotiable deals that Consumers can tailor to their needs." 
     flash[:notice3] = "You will be able to create, manage and administer your deals and your buyers in this easy to use control panel." 

     format.json { render json: pages_about_path, status: :created, location: @retailer } 
     else 
     @title = "Sign up" 
     format.html { render action: "new"} 
    flash[:notice1] = "Incorrect word verification. Are you sure you\'re human?" 
     format.json { render json: @retailer.errors, status: :unprocessable_entity } 
     end 


    end 

    csv_parse() 

    end 

上面的代碼被用來保存數據進入數據庫。 CSV文件@retailer.file_file_name是因爲它需要被解析到被存儲在數據庫以及和值需要存儲在字段

  1. csv_parse用來解析csvfile
  2. 我能夠保存文件數據

現在我需要解析csv文件並將各個字段存儲在另一個數據庫中。

csv_parse的代碼如下。

def csv_parse 

    @parsed_file=CSV.foreach(params[:dump][:file].original_filename) 

    n=0 
    @parsed_file.each do |row| 
     User_list.create(
    :email=>row[0], 
    :first_name=>row[1], 
    :last_name=>row[2]).save 

    flash.now[:message]="CSV parse Successful, #{n} new records added to data base" 
    end 
end 

當我運行它時,它給出了以下錯誤/秒。

  1. 你有沒有對象,當你沒有想到它!

  2. 您可能已經預期了Array的一個實例。

  3. 的錯誤,而評估零發生。[]

請幫我上面的錯誤爲什麼會拋出這樣的錯誤

在此先感謝。

+0

只是檢查,但你有檢查'params'確認':dump'和':dump [:file]'實際上是定義? – rjz 2012-03-17 16:10:01

+0

@rjz我對軌道上的紅寶石是全新的,我無法理解你想說什麼,請你更具體。非常感謝。 – shofee 2012-03-19 04:50:39

+0

@rjz請給出詳細的信息:dump – shofee 2012-03-19 06:09:44

回答

0

我只是粘貼代碼我工作的一些和平,並希望它會幫助你:

first_line = true 
file = File.open File.join(Rails.root, 'tmp/import/category.csv') 
file.each do |line| 
    unless first_line 
    row = CSV::parse_line(line, ';') 
    category = Category.find_by_name(row[1]) || Category.create(:name => row[1], :target => 'basic') 
    category.update_attribute(:import_id, row[0]) 
    else 
    first_line = false 
    end 
end 

我有時也寫了這個代碼導入類別,我的數據庫。在這裏,您可以更改CSV文件名和each迭代器的塊。另外first_line是領域的描述,我忽略它。

+0

我已經實現上傳文件,現在我需要解析它。 – shofee 2012-03-21 09:38:46

+0

我在這裏粘貼的代碼正是你想要的 – ka8725 2012-03-21 10:31:55

+0

好的非常感謝你的回覆,現在我將解釋我到底在做什麼。正如你可以看到有一個變量「@retailer」在「創建「他在裏面保存着許多領域,其中一個是csv文件的路徑,它保存在一個名爲file_file_name.now的數據庫中。我如何從變量」@retailer「提取路徑,以及如何將它傳遞給您功能,請儘快回覆我需要完成此操作,非常感謝您的回覆。 – shofee 2012-03-21 11:23:39

0
def create 
    @retailer = Retailer.new(params[:retailer]) 
    respond_to do |format|  
    # ... same as above 
    end 
    csv_parse(params[:retailer][:file].path) 

    private 

    def csv_parse(path) 
    rows = CSV.read(path) 
    rows.each do |row| 
     User_list.create(:email=> row[0], :first_name=>row[1], :last_name=>row[2]) 
    end 
    flash.now[:message]= "CSV parse Successful, #{rows.size} new records added" 
    end 
end