mardi 25 avril 2017

To maintain data in a form after doing a validation in rails

I am validating my form but sending the error message cleans the fields and I want the data to be kept so that I only complete the ones I need

I have this code in the controller:

def create
mensaje=""
@farm = Farm.new(params[:farm])
if @farm.nombre==""  || !valid_prescence?(@farm.nombre)
  mensaje="Favor de capturar los datos que se encuentran como requeridos"
else
  @buscar=Farm.where(nombre: params[:farm][:nombre], tipo: params[:farm][:cliente_id])
  if @buscar.any?
    mensaje="La finca "+params[:farm][:nombre]+" ya se encuentra registrada en el sistema, favor de verificar."
  end
end

respond_to do |format|
  if mensaje !=""
    format.html { redirect_to new_farm_path, notice: mensaje }
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  else
    if@farm.save
    format.html { redirect_to @farm, notice: 'Finca creada correctamente.' }
    format.json { render json: @farm, status: :created, location: @farm }
  else
    format.html { render action: "new" }
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  end
end end end

At the moment of falling in the exception returns me to the form but with empty fields What I want is to be able to keep the data after sending the message

Aucun commentaire:

Enregistrer un commentaire