Forum: Ruby on Rails Resubmitting remote form via AJAX -- old parameters get passed!!

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.
F05df05d7e52f4ad93cd0f9027818c75?d=identicon&s=25 gaveeno (Guest)
on 2009-03-10 23:15
(Received via mailing list)
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>&nbsp;</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
F05df05d7e52f4ad93cd0f9027818c75?d=identicon&s=25 gaveeno (Guest)
on 2009-03-10 23:33
(Received via mailing list)
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.
This topic is locked and can not be replied to.