error in select and autocomple ruby on rails

0

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

log

    
asked by juan gomez 15.06.2017 в 06:25
source

1 answer

0

The error is because your form is sending only the centro_costo_fullname attribute and your Reunion object needs the centro_costo_id attribute (this is because of the belongs_to :centro_costo association).

Then when trying to save the record in your controller (ie @reunion.save ), the validation fails since you are not giving that attribute (in rails 5 the belongs_to relationships require that the% id always be present unless indicated otherwise).

To solve it you have two options.

  • Modify your form to send centro_costo_id .
  • Get the id using centro_costo_fullname and assign it manually.
  • I recommend the second option because I think it's simpler. For this you need to obtain the cost center from your model and assign its id to @reunion .

    For example, you could get the cost center 1 :

    centro_costo = CentroCosto.find_by(fullname: reunion_params[:centro_costo_fullname])
    

    And then assign the id to @reunion :

    @reunion.centro_costo_id = centro_costo.id
    

    Then, this would look like the action create of the controller:

    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
    

    1 I assume that your reunion_params method allows the centro_costo_fullname attribute.

    In case the validation continues to fail, you can modify your code to show an error instead of returning a false (which in your log is identified only by rollback ).

    To do so, in the action create of the controller, change this line:

    if @reunion.save
    

    For this one:

    if @reunion.save!
    

    And notice the error that will be displayed in the console when you try to create a Reunion .

    Once you identify the error you must return the code of your action create to @reunion.save , otherwise users will be able to get error screens when they enter the wrong data (instead of messages friendlier).

        
    answered by 15.06.2017 / 07:19
    source