Forum: Ruby on Rails validations erasing the form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4f7fb063aecdf8344774cd7238d8b585?d=identicon&s=25 Jorge Mario G. Mazo (Guest)
on 2006-05-09 19:19
(Received via mailing list)
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
40a94646462b58d8b0109b5fa6655c72?d=identicon&s=25 Jonathan Conway (Guest)
on 2006-05-09 20:21
(Received via mailing list)
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
4f7fb063aecdf8344774cd7238d8b585?d=identicon&s=25 Jorge Mario G. Mazo (Guest)
on 2006-05-09 22:51
(Received via mailing list)
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? %>
       <div class="errors">
       <p>No se pudo proseguir:</p>
       <ul>
          <% @usuario.errors.each_full do |msg| %>
            <li><%= errortraductor(msg) %></li>
            <!-- agregar un traductor como traducir(msg) y
                lo ponga lindo, OJALA UN HELPER -->
          <%  end %>
       </ul>
       </div>
    <% end %>
<%= start_form_tag :action => 'agregadoUsuario' %>
<br><b><center><%= flash[:notice] %></b></center>
  <%= render :partial => 'update' %>
 <center> <%= submit_tag("Agregar Usuario") %></center>
<%= end_form_tag %>
40a94646462b58d8b0109b5fa6655c72?d=identicon&s=25 Jonathan Conway (Guest)
on 2006-05-09 23:37
(Received via mailing list)
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
This topic is locked and can not be replied to.