0

このチュートリアルCSV-FILE-EXPORT-IMPORT- RAILS に従っていますが、オブジェクトの初期化されていない定数 CuentaContablesController::Falseを作成しようとしているときにばかげたエラーが発生したため、何かが間違っています。問題なくファイルを読み取ることができますが、このエラーが頭を悩ませています! どんな助けでも大歓迎です!

私のコントローラー(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
4

1 に答える 1

2

You're using True instead of true (likewise for false).

But neither are necessary; the ternary is superfluous and over-parenthesized:

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

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

You might even use a helper to turn row 5 into a boolean and remove it from the mainline code.

于 2012-07-11T22:34:14.710 に答える