gaveeno
2009-Mar-10 22:14 UTC
Resubmitting remote form via AJAX -- old parameters get passed!!
When I submit a form via AJAX and get back some errors in my "errors" div, if I try to resubmit the form with the errors corrected it still passes the old parameters. See my code at: http://pastie.org/413269 (also pasted below). I can''t figure out why the 2nd time the form is submitted, it still has the old parameters even though the fields are populated with the new parameters. Anybody know why this could be? ## views/campaigns/_edit.html.erb <% remote_form_for @campaign, :url => {:controller => :setup, :action => :update_campaign, :id => @campaign },:html => {:id => ''campaign_form''}, :loading => "Element.show(''campaign_spinner'');", :complete => "Element.hide(''campaign_spinner'');" do |campaign_form| %> <%= render :partial => ''campaigns/edit_form_contents'', :locals => {:form_type => form_type,:campaign_form => campaign_form} %> <% end %> ## views/campaigns/_edit_form_contents.html.erb <div id = "errors"></div> <div class = "span-18 last first-row"> <div class = "span-2"> <img src = ''/images/1_25px.png'' class = ''number''/> </div> <div class = "span-8 last"> <%= campaign_form.label :description, "Description" %> <%= campaign_form.text_field :description, :class => ''custom span-8'' %> </div> </div> <div class = "span-18 last row"> <div class = "span-2"> <img src = ''/images/2_25px.png'' class = ''number''/> </div> <div class = "span-5"> <%= campaign_form.label :start_date, "Start date" %> <%= campaign_form.calendar_date_select :start_date, :time => false, :class => "span-4" %> </div> </div> <div class = "span-18 last" style="position:relative;"> <div class = "span-4 append-1 button-container"> <%= button_submit_to_remote "Save",{:icon=>"accept",:submit_form => ''campaign_form'',:size => "small"},"left","campaign_spinner" %> </div> <div class = "span-17 last"> <img style = "display:none;position:absolute;right:5px;top:10px;" id = "campaign_spinner" src = "/images/spinners/ spin_small_dark_blue.gif"> </div> </div> ## helpers/setup_helper.rb def button_submit_to_remote(name,options{},alignment=nil,spinner=nil) path = {} position = get_position(alignment) img_tag = get_image(options.delete(:icon) || "") button_type = get_button_size(options.delete(:size) || "") link = (img_tag ||= "") + name + "<span> </span>" options.store(:class,"#{button_type} #{position}") submit_form = options.delete(:submit_form) || nil if submit_form link_to_function link, options, :class => "#{button_type} # {position}" do |page| page << "Element.show(''#{spinner}'');" if spinner page << "$(''#{submit_form}'').onsubmit()" end end end ## controllers/campaigns_controller.rb def update @campaign = Campaign.find(params[:id]) respond_to do |format| if @campaign.update_attributes(params[:campaign]) flash[:success] = ''Updates saved.'' format.html { redirect_to setup_campaigns_path } format.js { redirect_to setup_campaigns_path } else flash.now[:error] = "Couldn''t save campaign." format.html { redirect_to :action => ''edit''} format.js { render :update do |page| page.replace_html :errors, error_messages_for([:campaign]) page << "window.scrollTo(0,0);" end } end end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gaveeno
2009-Mar-10 22:32 UTC
Re: Resubmitting remote form via AJAX -- old parameters get passed!!
I realized that the form is actually sending the new parameters but I shouldn''t be using a remote form here if I want a redirect...the format.js {redirect_to setup_campaigns_path } doesn''t actually redirect the browser. On Mar 10, 3:14 pm, gaveeno <gavin.to...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I submit a form via AJAX and get back some errors in my "errors" > div, if I try to resubmit the form with the errors corrected it still > passes the old parameters. See my code at: http://pastie.org/413269 > (also pasted below). I can''t figure out why the 2nd time the form is > submitted, it still has the old parameters even though the fields are > populated with the new parameters. Anybody know why this could be? > > ## views/campaigns/_edit.html.erb > <% remote_form_for @campaign, :url => {:controller => :setup, :action > => :update_campaign, :id => @campaign },:html => {:id => > ''campaign_form''}, > :loading => "Element.show(''campaign_spinner'');", > :complete => "Element.hide(''campaign_spinner'');" do |campaign_form| > %> > <%= render :partial => ''campaigns/edit_form_contents'', :locals => > {:form_type => form_type,:campaign_form => campaign_form} %> > <% end %> > > ## views/campaigns/_edit_form_contents.html.erb > <div id = "errors"></div> > <div class = "span-18 last first-row"> > <div class = "span-2"> > <img src = ''/images/1_25px.png'' class = ''number''/> > </div> > <div class = "span-8 last"> > <%= campaign_form.label :description, "Description" %> > <%= campaign_form.text_field :description, :class => ''custom span-8'' > %> > </div> > </div> > <div class = "span-18 last row"> > <div class = "span-2"> > <img src = ''/images/2_25px.png'' class = ''number''/> > </div> > <div class = "span-5"> > <%= campaign_form.label :start_date, "Start date" %> > <%= campaign_form.calendar_date_select :start_date, :time => > false, :class => "span-4" %> > > </div> > </div> > <div class = "span-18 last" style="position:relative;"> > <div class = "span-4 append-1 button-container"> > <%= button_submit_to_remote "Save",{:icon=>"accept",:submit_form => > ''campaign_form'',:size => "small"},"left","campaign_spinner" %> > </div> > <div class = "span-17 last"> > <img style = "display:none;position:absolute;right:5px;top:10px;" id > = "campaign_spinner" src = "/images/spinners/ > spin_small_dark_blue.gif"> > </div> > </div> > > ## helpers/setup_helper.rb > > def button_submit_to_remote(name,options> {},alignment=nil,spinner=nil) > path = {} > position = get_position(alignment) > img_tag = get_image(options.delete(:icon) || "") > button_type = get_button_size(options.delete(:size) || "") > link = (img_tag ||= "") + name + "<span> </span>" > options.store(:class,"#{button_type} #{position}") > submit_form = options.delete(:submit_form) || nil > > if submit_form > link_to_function link, options, :class => "#{button_type} # > {position}" do |page| > page << "Element.show(''#{spinner}'');" if spinner > page << "$(''#{submit_form}'').onsubmit()" > end > end > end > > ## controllers/campaigns_controller.rb > > def update > @campaign = Campaign.find(params[:id]) > > respond_to do |format| > if @campaign.update_attributes(params[:campaign]) > flash[:success] = ''Updates saved.'' > format.html { redirect_to setup_campaigns_path } > format.js { redirect_to setup_campaigns_path } > else > flash.now[:error] = "Couldn''t save campaign." > format.html { redirect_to :action => ''edit''} > format.js { > render :update do |page| > page.replace_html :errors, error_messages_for([:campaign]) > page << "window.scrollTo(0,0);" > end } > end > end > end--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---