Ajax


#1

Hi, I need some help. I`m developing a data base administrator using
ruby and ajax, both for the first time and I have a problem:
I have a form where you can add or delete some information about a
person., information related with their studies. There is no problem
when I add, but when I delete, it deletes from the database but when I
try to save it throughs me an error because it tries to find the deleted
information.

Here is what i have.

_form.rhtml:

 <tr>
  <td><strong>Estudios:</strong></td>
  <td>
      <%= link_to_remote 'Agregar Estudio',

:url=>{:action=>‘agregar_estudio’,:id => @persona},
:update=>‘nuevo_estudio’,:position
=> :top %>

 

<% @persona.estudios.each do |@e|%>

Grado:<%= text_field (‘e[]’,‘grado’)%>
Institucion:<%= text_field
(‘e[]’,‘institucion’) %>
Año:
<% if false %> <%= date_select(‘e[]’, ‘ano’,
:start_year => 1960,:discard_day => true, :discard_month => true)%><%
end %>


<%= link_to_remote ‘Eliminar Estudio’, :url=>{:action =>
‘eliminar_estudio’, :id => @persona, :estudio_id => @e},
:update=>'estudio '+ @e.id.to_s,:position=>:top%>


<% end %>
     <tr id="nuevo_estudio">
         <td colspan="4">&nbsp;</td>
    </tr>

The year is commented because I have problems with it too, I created
another topic about it.

personas_controller.rb:

def eliminar_estudio
persona = Persona.find(params[:id])
estudio_temp = Estudio.find(params[:estudio_id])
@id_for_div = "estudio "+estudio_temp.id.to_s
persona.estudios.delete(estudio_temp)
render :action=> ‘…/application/eliminar_item’

end

def agregar_estudio
@nuevo_estudio = session[:ne]
ne = Estudio.new
@nuevo_estudio << ne
session[:ne] = @nuevo_estudio
render :partial => ‘estudio’
end
def update
@persona = Persona.find(params[:id]) if !params[:grado].nil?
i=0
params[:grado].each do
@persona.estudios << Estudio.new(:grado => params[:grado][i],
:institucion => params[:institucion][i])
i+=1
end
end
Estudio.update(params[:e].keys,params[:e].values) unless params[:e].nil?
if @persona.update_attributes(params[:persona])
@persona.save!
flash[:notice] = ‘El contacto ha sido modificado
exitosamente’
redirect_to :action => ‘show’, :id => @persona
else
flash[:notice] = ‘El contacto NO ha sido modificado, revise la
información’
redirect_to :action => ‘show’, :id=> @persona
end
end


eliminar_item.rjs:

page[@id_for_div].visual_effect :highlight
page[@id_for_div].visual_effect :blind_up


_estudio.rhtml:

Grado:<%= text_field_tag 'grado[]'%> Institucion:<%= text_field_tag 'institucion[]' %> Año: <%= select_year(Date.today, :start_year => 1950, :name => 'ano')%>

Please… help me!!!