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"> </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ñ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"> </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ño:</strong> <%= select_year(Date.today, :start_year => 1950, :name => ''ano'')%> </td> </tr> Please... help me!!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Is there a way to update form field data using Ajax? For example, I want a select field with, say, phone numbers in a form. Then I''ll use observe_field to monitor changes, and when the user selects a phone number, the ajax call will update a description field in the same form with, say, the person''s name related to the phone number. I''ve successfully got the ajax call working, and, since the second field is in a table, I''ve given the table cell an id and told the ajax call to update that cell. It then rebuilds the input field with the new data. It all works fine, and the new data appears in the correct field as expected, but when I post the form, it treats the description field as empty. I''ve tried submitting the form by just typing data in the description field, and it works correctly, but if I select something in the select field and it updates the description field, the form always treats it as empty. What am I doing wrong? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shawn wrote:> Is there a way to update form field data using Ajax? For example, I > want a select field with, say, phone numbers in a form. Then I''ll use > observe_field to monitor changes, and when the user selects a phone > number, the ajax call will update a description field in the same form > with, say, the person''s name related to the phone number. > (.....)You can simply change the field value page[''your_object_your_field''].value=@your_object.your_field For select tag, the value to set is the option ID. If you need to replace select values: page[''field_id''].length=0 @your_object.each do |obj| page << "$(''field_id'').options[$(''field_id'').length]=new Option(''#{escape_javascript(obj.to_s)}'',#{obj.id})" end Regards Massimo http://www.addsw.it -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. It sounds like exactly what I''m trying to do, and forgive my ignorance, but I still can''t get it to work.> You can simply change the field value > > page[''your_object_your_field''].value=@your_object.your_field >Where do I put the above statement? Is it part of the observe_field statement (maybe as a parameter for :complete)? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shawn wrote:> Thanks for the reply. It sounds like exactly what I''m trying to do, and > forgive my ignorance, but I still can''t get it to work. > >> You can simply change the field value >> >> page[''your_object_your_field''].value=@your_object.your_field >> > > Where do I put the above statement? Is it part of the observe_field > statement (maybe as a parameter for :complete)?No; you can put the code directly in your controller method def phone_changed desc=(params[:phone]) render :update do |page| page[''your_object_your_field''].value=desc end end or as a separate rjs template view (phone_changed.rjs), assuming you set @desc in controller page[''your_object_your_field''].value=@desc -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Massimo wrote:> > No; you can put the code directly in your controller method > > def phone_changed > desc=(params[:phone]) > render :update do |page| > page[''your_object_your_field''].value=desc > end > end > > or as a separate rjs template view (phone_changed.rjs), assuming you set > @desc in controller > > page[''your_object_your_field''].value=@descOk, got it. I wasn''t aware of the :update form of render. (Ok, to be honest, Ajax and java and rjs templates are still pretty foggy generally.) Anyway, thanks a lot for the help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Shawn, Shawn wrote:> (Ok, to be honest, Ajax and java and rjs templates > are still pretty foggy generally.)I recommend Cody Fauser''s RJS tutorial, available in PDF for from O''Reilly for $10. Best ten bucks I''ve spent on Rails so far. It won''t make you an expert, but I found it to be a dang good starting point. hth, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Massimo wrote:> If you need to replace select values: > > page[''field_id''].length=0 > @your_object.each do |obj| > page << "$(''field_id'').options[$(''field_id'').length]=new > Option(''#{escape_javascript(obj.to_s)}'',#{obj.id})" > end >Just an fyi for anyone else who might be following this, I had to change Option(''#{escape_javascript(obj.to_s)}'',#{obj.id})" to Option(''#{escape_javascript(obj.description.to_s)}'',#{obj.id})" in order for it to work. Thanks again Massimo. Bill, thanks for the tutorial recommendation. My brain balks at the idea of me trying to stuff some other new knowledge in there, but it''s probably inevitable. Shawn -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---