2012-07-11 96 views
0

我下面這個教程CSV-FILE-EXPORT-IMPORT-RAILS但一些即時通訊做錯了,因爲我得到了,我要創建我的對象未初始化的常量CuentaContablesController ::假一個愚蠢的錯誤。我可以毫無問題地閱讀文件,但是這個錯誤讓我很頭疼!任何幫助將不勝感激!Ruby on Rails的 - 導入CSV文件

在我的控制器(cuenta_contable_controller.rb)中導入的方法如下所示;

class CuentaContablesController < ApplicationController  
.... 
def upload(params) 
logger.info "**File loaded***" 
infile = params[:file].read 
n, errs = 0, [] 
@archivo = [] 
SV.parse(infile) do |row| 
     n += 1 
     # SKIP: header i.e. first row OR blank row 
     next if n == 1 or row.join.blank? 
     cuenta_contable = CuentaContable.build_from_csv(row) 
     if cuenta_contable.valid? 
     cuenta_contable.save 
     @archivo << row 
     else 
     errs << row 
     end 
    end 
    logger.info errs 
    flash[:success] = "Las cuentas contables fueron cargadas." 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @archivo } 
    end 
    end 

我的模型(cuenta_contable.rb)這樣

class CuentaContable < ActiveRecord::Base 
.... 
def self.build_from_csv(row) 
    ultimo_nivel = (row[5].downcase=="si") ? (True):(False) 
    #cuenta = find_or_initialize_by_cuenta("#{row[0]}-#{row[1]}-#{row[2]}") 
    # Buscas el archivo existing customer from email or create new 
    cuenta = CuentaContable.new(:cuenta => "#{row[0]}-#{row[1]}-#{row[2]}", 
           :descripcion => "#{row[3].titleize}", 
           :categoria_cuenta => "#{row[4].titleize}", 
           :ultimo_nivel=> ultimo_nivel) 
    return cuenta 
    end 

回答

2

您使用的trueTrue代替(同樣爲false)。

但都不是必需的;三元是多餘的,過度括號:

# Ick! 
ultimo_nivel = (row[5].downcase=="si") ? (True):(False) 

# Pretty! 
ultimo_nivel = row[5].downcase == "si" 

你甚至可能使用一個輔助開啓第5行成一個布爾值,並從主線代碼中刪除。

+0

明白了!你是對的......謝謝! – 2012-07-11 22:38:06