Forum: Ruby on Rails Ajax

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.
Chika (Guest)
on 2007-01-23 20:21
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:


<!-- estudios --------------------------------------------->

     <tr>
      <td><strong>Estudios:</strong></td>
      <td>
          <%= link_to_remote 'Agregar Estudio',
:url=>{:action=>'agregar_estudio',:id => @persona},
                                                :update=>'nuevo_estudio',:position
=> :top %>
            </td>
      <td colspan="2">&nbsp;</td>
    </tr>
    <% @persona.estudios.each do |@e|%>
    <tr id="estudio <%=@e.id%>">
      <td><strong>Grado:</strong><%= text_field ('e[]','grado')%></td>
            <td><strong>Institucion:</strong><%= text_field
('e[]','institucion') %></td>
            <td><strong>A&ntilde;o:</strong>
<% if false %>                        <%= date_select('e[]', 'ano',
:start_year => 1960,:discard_day => true, :discard_month => true)%><%
end %>
            </td>
            <td>
               <%= link_to_remote 'Eliminar Estudio', :url=>{:action =>
'eliminar_estudio', :id => @persona, :estudio_id => @e},
               :update=>'estudio '+ @e.id.to_s,:position=>:top%>
             </td>
         </tr>
         <% end %>

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

<!-- termina estudios -------------------------------------->
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:


<tr>
    <td>
        <strong>Grado:</strong><%= text_field_tag 'grado[]'%>
    </td>
    <td>
        <strong>Institucion:</strong><%= text_field_tag 'institucion[]'
%>
    </td>
    <td>
        <strong>A&ntilde;o:</strong>
                         <%= select_year(Date.today, :start_year =>
1950, :name => 'ano')%>
    </td>
</tr>

Please... help me!!!
This topic is locked and can not be replied to.