Hi, I'm doing a form and it requires an autocomplete and selects but in the paths; When I'm going to save, I get an error.
the screen pulls an error remains in validation. I put the Log
My code:
Model
reunion.rb
class Reunion < ApplicationRecord
belongs_to :centro_costo
def centro_costo_fullname
centro_costo.fullname if centro_costo
end
def centro_costo_fullname=(fullname)
self.centro_costo = CentroCosto.find_by_fullname(fullname) unless fullname.blank?
end
end
Routes
resources :reuniones do
collection do
get :autocomplete_centro_costo_fullname
get :select_region
get :select_ciudad
get :select_planta
end
end
driver
class ReunionesController < ApplicationController
before_action :set_reunion, only: [:show, :edit, :update, :destroy]
autocomplete: cost_center,: fullname,: full = > true,: column_name = > 'fullname'
# GET /reuniones
# GET /reuniones.json
def index
@reuniones = Reunion.all
@negocios = Negocio.all
end
def select_region
rs = Region.where(:negocio_id => params[:idnegocio]).order('nombre').all
respond_to do |format|
format.json {render json: rs }
format.html
end
end
def select_ciudad
rs = Ciudad.where(:region_id => params[:idregion]).order('nombre').all
respond_to do |format|
format.json {render json: rs }
format.html
end
end
def select_planta
rs = Planta.where(:ciudad_id => params[:idciudad]).order('nombre').all
respond_to do |format|
format.json {render json: rs }
format.html
end
end
def new
Time.zone = 'America/Bogota'
@reunion = Reunion.new(fecha_entrega: Time.zone.now.strftime("%d/%m/%Y"))
@reunion.detalles_reuniones.build
@negocios = Negocio.all #para javascript
end
# GET /reuniones/1/edit
def edit
end
# POST /reuniones
# POST /reuniones.json
def create
centro_costo = CentroCosto.find_by(fullname:reunion_params[:centro_costo_fullname])
@reunion = Reunion.new(reunion_params)
@reunion.centro_costo_id = centro_costo.id
respond_to do |format|
if @reunion.save!
format.html { redirect_to @reunion, notice: 'Reunion was successfully created.' }
format.json { render :show, status: :created, location: @reunion }
else
@negocios = Negocio.all
format.html { render :new }
format.json { render json: @reunion.errors, status: :unprocessable_entity }
end
end
end
end centro_costo is an association.
private
# Use callbacks to share common setup or constraints between actions.
def set_reunion
@reunion = Reunion.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reunion_params
params.require(:reunion).permit(:hora_pedido, :fecha_pedido,
:hora_inicio, :hora_final, :fecha_entrega, :observacion, :subtotal,
:planta_id, :ubicacion,:centro_costo_fullname,:hora_entre,:nombre,
#Aca esta el maestro de detalle.
detalles_reuniones_attributes: [:id,:reunion_id, :cantidad, :valor, :producto_id, :hora_entrega,:_destroy])
end