0

我正在尝试将文件导入 excel,但是当我尝试使用相关数据创建记录时,它向我显示以下错误:

no implicit conversion of Symbol into Integer  

在这一行:

 list.detallelp_attributes = {Articulo: row["Articulo"], Minimo: row["Minimo"], Maximo: row["Maximo"], IdEmpresa: empresa}

这是我的列表控制器和我用来导入的方法:

 has_many :detallelp, class_name: "Deta", foreign_key: "ListaId"
  accepts_nested_attributes_for :detallelp


  def self.import(file,empresa)#importar
    @errors = []
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|

      row = Hash[[header, spreadsheet.row(i)].transpose]
      list = find_by_id(row["id"]) || new
      list.attributes = {id: row["id"], Lista: row["Nombre"], Tipo: row["Tipo"], FechaIni: row["Fecha inicial"], FechaFin: row["Fecha final"], IdEmpresa: empresa}

      list.detallelp_attributes = {Articulo: row["Articulo"], Minimo: row["Minimo"], Maximo: row["Maximo"], IdEmpresa: empresa}

      if list.save
        # stuff to do on successful save
       else
         list.errors.full_messages.each do |message|
           @errors << "Error fila #{i}, columna #{message}"
         end
       end


    end
    @errors #  <- need to return the @errors array
  end

我在列表控制器中的方法导入:

  def import
    empresa = current_usuario.empresa_id
    @errors = List.import(params[:file], empresa)
    if @errors.present?
      render :errorimportation; #Redirije a dicha vista para mostrar los errores
      return;
    else
      redirect_to listap_path, notice: "listas importadas."
    end
  end

这是我的“deta”控制器:

  belongs_to :list, class_name:"List", foreign_key: "ListaId"
4

1 回答 1

1

你的关系是has_many。因此,accepts_nested_attributes_for需要一个数组。你给它一个哈希值。这就是错误的根源。给它一个数组。

于 2017-02-02T23:31:40.537 回答