Validations erasing the form


#1

Hi there
I have a for with a couple of text_fields. I’m doing some validation
via the model and when the validation “works” I get there was an error
on blah blah
the problem is the form is erased and I have to fill up the form again!
I saw that scaffold keeps the form filled when the validation “works”
but I dont know exactly what’s making it happen!
I would like to know how to make this work

Thanks in advance
Jorge


#2

You need to have the model assigned to an instance variable that is
named the same as the model parameter that you pass into the text_field
helper. This way the variable is within scope of the view and can be
used by the text_field helper.

Controller

def create
@cheese = Cheese.new(params[:cheese])
if @cheese.save
#do whatever on success
else
render 'your_view
end
end

View

<%= text_field ‘cheese’, ‘name’ %>

Cheers

Jonathan

http://www.agileevolved.com


#3

Hi there an thanks for answer,
I have it just like that but still does not work here is my data
I dont know what i’m doing wrong

def ingresarUsuario
@usuario = Usuario.new
@cursoslist = Cursos.find(:all)
@tipo = Tipousuario.sacar
end

def agregadoUsuario
@cursoslist = Cursos.find(:all)
@tipo = Tipousuario.sacar
@uid = params[:usuarios][:strid]
@cur = params[:cursostomados]

#-------------------- insertamos el usuario primero -------
a = Usuario.randompass(8)

@k = {
	"strnombres"   =>  params[:usuarios][:strnombres],
	"strapellidos" =>  params[:usuarios][:strapellidos],
	"strid"	       =>  params[:usuarios][:strid],
	"stremail"     =>  params[:usuarios][:stremail],
	"inttipo"      =>  params[:usuarios][:inttipo],
	"strdireccion" =>  params[:usuarios][:strdireccion],
	"strtelefono"  =>  params[:usuarios][:strtelefono],
	"strlogin"     =>  params[:usuarios][:strlogin],
	"strclave"     =>  a
	}

	@usuario = Usuario.new(@k)
	#si el usuario crea, entonces lo agregamos a cursosxusuarios
	if @usuario.save
        usuario = 

Usuario.find_by_stremail(params[:usuarios][:stremail])
RegistroMail.deliver_bienvenido(usuario)
if @cur
@cur.each do |cursos_id, do_it|
if do_it == ‘1’
datas = Hash.new
datas = {“idusuario” => @uid,
“idcurso” => cursos_id
}
@cursosxusuario = Cursosxusuario.new(datas)
if @cursosxusuario.save
#agreguame un error en caso de que falle

 		      else
    	        flash[:notice] = "Error al matricular usuario al curso"
                render :action => 'buscarusuario'
 		      end
		   end
		  end
        end
        flash[:notice] = "Usuario Agregado al Sistema"
        redirect_to :action => 'ingresarUsuario'
	else
		flash[:notice] = "Hubo errores en la forma, Revisela e intente de 

nuevo"
render :action => ‘ingresarUsuario’
end

end
–and here is the view–
<% unless @usuario.errors.empty? %>


No se pudo proseguir:



    <% @usuario.errors.each_full do |msg| %>
  • <%= errortraductor(msg) %>


  • <% end %>


<% end %>
<%= start_form_tag :action => ‘agregadoUsuario’ %>

<%= flash[:notice] %>
<%= render :partial => ‘update’ %> <%= submit_tag("Agregar Usuario") %> <%= end_form_tag %>

#4

I think I see your problem. Instead of doing
render :action => ‘ingresarUsuario’
replace this with
render ‘ingresarUsuario’ in the last part of the function where you deal
with errors.

Cheers

Jonathan

http:/www.agileevolved.com