I would like to get the details of a header and I have them all related, I just need to show them in the same view, try to add it but it says undefined method cod_prod for nil:NilClass
<p id="notice"><%= notice %></p>
<p>
<strong>Numero oc:</strong>
<%= @encabezado.numero_oc %>
</p>
<p>
<strong>Tipo oc:</strong>
<%= @encabezado.tipo_oc %>
</p>
<p>
<strong>Fecha g:</strong>
<%= @encabezado.fecha_g %>
</p>
<p>
<strong>Fecha en:</strong>
<%= @encabezado.fecha_en %>
</p>
<p>
<strong>Cod prod:</strong>
<%= @detalle.cod_prod %>
</p>
<p>
<strong>Numero item:</strong>
<%= @detalle.numero_item %>
</p>
<p>
<strong>Descrip:</strong>
<%= @detalle.descrip %>
</p>
If there is a simpler way, it would be great. I want to finish this project .. I am new, maybe the question is something obvious for you but I am a little bit complicated with the subject
driver:
detalles
class Detalle < ApplicationRecord
belongs_to :encabezado , optional: true
end
encabezado
class Encabezado < ApplicationRecord
has_many :detalles
end
Header driver:
class EncabezadosController < ApplicationController
before_action :set_encabezado, only: [:show, :edit, :update, :destroy]
# GET /encabezados
# GET /encabezados.json
def index
@encabezados = Encabezado.all
end
# GET /encabezados/1
# GET /encabezados/1.json
def show
@e = nose
end
# GET /encabezados/new
def new
@encabezado = Encabezado.new
end
# GET /encabezados/1/edit
def edit
end
# POST /encabezados
# POST /encabezados.json
def create
@encabezado = Encabezado.new(encabezado_params)
respond_to do |format|
if @encabezado.save
format.html { redirect_to @encabezado, notice: 'Encabezado was successfully
created.' }
format.json { render :show, status: :created, location: @encabezado }
else
format.html { render :new }
format.json { render json: @encabezado.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /encabezados/1
# PATCH/PUT /encabezados/1.json
def update
respond_to do |format|
if @encabezado.update(encabezado_params)
format.html { redirect_to @encabezado, notice: 'Encabezado was successfully updated.' }
format.json { render :show, status: :ok, location: @encabezado }
else
format.html { render :edit }
format.json { render json: @encabezado.errors, status: :unprocessable_entity }
end
end
end
# DELETE /encabezados/1
# DELETE /encabezados/1.json
def destroy
@encabezado.destroy
respond_to do |format|
format.html { redirect_to encabezados_url, notice: 'Encabezado was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_encabezado
@encabezado = Encabezado.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list
through.
def encabezado_params
params.require(:encabezado).permit(:numero_oc, :tipo_oc, :fecha_g, :fecha_en, :fecha_list, :d_comprador, :lugar_entrega, :emisor, :inf_comprador, :codic_pago, :diascredito, :diasentrega, :status_oc, :text)
end
def nose
File.foreach('abc.txt').with_object([]) do |line, result|
puts line
if line.start_with?('UNH')
clave1 = 'ORDEN DE COMPRA'
end
if line.start_with?('BGM')
clave2 = line[0..2]
@codigooc = line[6...26]
if line.include?('BGM2')
tipooc = 'ORDEN DE COMPRA '
cajas = line[-1]
else
tipooc = 'ORDEN DE COMPRA SUGERIDA'
end
if cajas = '1'
@dtipooc = 'CANCELACION'
end
if cajas = '9'
@dtipooc = 'ORIGINAL'
end
if cajas = '6'
@dtipooc = 'PROPUESTA'
end
end#line GBM
if line.start_with?('DTM137')
fechag2 = Date.new.strftime("%Y/%m/%d")
clave = line[0..5]
fechag = line[6...14]
@fechag2 = fechag
end
if line.start_with?('DTM ')
fechaen2 = Date.new.strftime("%Y/%m/%d")
clave = line[0..2]
fechaen = line[6...14]
@fechaen2 = fechaen
end
if line.start_with?('DTM157')#
fechalist2 = Date.new.strftime("%Y/%m/%d")
fechalist = line[6...14]
@fechalist2 = fechalist
end
if line.start_with?('NADBY')
clave = line[0..4]
@comprador = line[6...-1]#valor original 6..1 modificado por pruebebas
end
if line.start_with?('NADSU')
clave = line[0..4]
@proveedor = line[6...-1]
end
if line.start_with?('CTAOC') and line.include?('LA VINOTECA LIMITADA') or line.include?('AVDA.MANUEL MONT 1452') or line.include?('3433607')
clave = line[0..4]
@emisor = "LA VINOTECA MADURO"
end
if line.start_with?('CTAOC') and !line.include?('LA VINOTECA LIMITADA') and !line.include?('AVDA.MANUEL MONT 1452') and !line.include?('3433607')
@emisor2 = line[6...-1]
end
if line.start_with?('PAT')
clave = line[0..2]
tpago = line[5]
dentrega = line[7..8]
dpagar = line [-3..-1]
result << "#{clave} , codigo #{tpago} , condiciones normales #
{tpago} , dias para entrega #{dentrega} , Dias para pagar #{dpagar} "
end
if line.start_with?('TODNC')
clave = line[0..4]
@flete1 = 'flete por cuenta del vendedor sin costo'
else
@flete1 = 'flete por cuenta del comprador'
end
if line.start_with?('LOC')
clave = line[0..2]
lugar = line[5]
codigolugar = line[6...-1]
@codigolugar2 = codigolugar
end
#Encabezado.create( numero_oc:@codigooc , tipo_oc:@dtipooc ,
fecha_g:@fechag2 , lugar_entrega:@codigolugar2 , inf_comprador:@emisor2)
end#do
@p = Encabezado.new( numero_oc:@codigooc , tipo_oc:@dtipooc ,
fecha_g:@fechag2 , lugar_entrega:@codigolugar2 , inf_comprador:@emisor2 ,
fecha_en: @fechaen2 , fecha_list: @fechalist2 , d_comprador:@comprador)
#= Detalle.new
File.foreach('abc.txt').with_object([]) do |line, result|
puts "procensando linea #{line} ..."
case
when line.start_with?('LIN')
@cproducto = line[9..-2]
@numeroitem = line[7..8]
@numeroitem2 = @numeroitem.to_s
when line.start_with?('IMD')
@desproducto = line[8..-1]
when line.start_with?('QTY 21')
@cantidadpedida = line[19..20]
caja = line[-3..-1]
pedido = '?????????????????????'
when line.start_with?('QTY129')
@cantidadunidades = line[-6..-4]
@tunidad = 'Unidades Simples'
#Detalle.last.destroy
when line.start_with?('MOA203') # not ('MOA 203')
@precio = 'Precio neto'
@precioneto = line[7..-1] # not [-10..-1
@precioneto2 = @precioneto.to_f.round(3).to_f
#Detalle.create(cproducto: @cproducto, cantidadunidades: @cantidadunidades,
desproducto:@desproducto, precio: @precioneto)
when line.start_with?('MOA 204')
@tdescu = 'Descuento'
@valordescu = line[-10..-1]
when line.start_with?('PRIAAA')
pedido = line[-3..-1]
@valorcd = line[-16..-8]
# @tcaja2 = unitOfMeasure(pedido)
when (line.start_with?('ALCA') and (line.include?('DE1') or
line.include?('DE2')))
@cdescuento = 'DESCUENTO'
when line.start_with?('ALCC') && (line.include?('CA1') ||
line.include?('CA1'))
@ccargo = 'CARGO'
when line.start_with?('PCD')
if line[5] = '1'
then
porcentaje = line[-6..-1] #
@porecentaje = 'porcentaje de descuento'
else
@porecentaje = 'porcentaje del cargo'
end
when line.start_with?('MOA 23')
@tcargo = 'Cargo'
@valorcargo = line[-10..-1]
@p.detalles.create(cod_prod:@cproducto,descrip:@desproducto,
numero_item:@numeroitem2, cantidadpedida:@cantidadpedida , precio_unit:
@precioneto2)
when line.start_with?('MOA 86')
clave = line[0..2]
importe = line[-9..-1]
when line.start_with?('CNT')
@items2 = line[-2..-1]
else
puts 'empieza con otra cosa'
end
def unitOfMeasure(p_pedido)
case p_pedido
when 'CS' then 'cajas'
when 'CSC' then 'cajas de carton'
when 'SIN' then 'Unidades Simples'
when 'KGM' then 'Kilogramos'
else
puts "bad unit #{p_pedido}"
'undefined'
end
end
@p.save
end
@p
end
#end nose
#puts nose('ojos.txt')
end