title: Rails how to submit a form with ajax and include some jquery variables
body: <p>I'm trying to submit a form via ajax and include some variables that i'm setting via jquery.</p>
  
  <p>/views/applications/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@application, :html =&gt; {:class =&gt; "organizer"}, :remote =&gt; true) do |f| %&gt;
    // fields in here
  &lt;% end %&gt;
  </code></pre>
  
  <p>/controllers/applications_controller.rb</p>
  
  <pre><code>  # PUT /applications/1
    # PUT /applications/1.json
    def update
      @application = Application.find(params[:id])
      p params[:xposition]     # these get set in jquery
      p params[:application]   # these get set in jquery
      respond_to do |format|
        if @application.update_attributes(params[:application])
          @curr_app = ApplicationField.last
  
          format.html { redirect_to @application, notice: 'Application was successfully updated.' }
          format.json { head :no_content }
          format.js { render action: "update" }
        else
          format.html { render action: "edit" }
          format.json { render json: @application.errors, status: :unprocessable_entity }
          format.js { render action: "update" }
        end
      end
    end
  </code></pre>
  
  <p>/assets/javascripts/applications.js</p>
  
  <pre><code>$('.organizer').submit(function() {
  
          var dataToSubmit = $(this).serialize();
          var field_values = [];
          var x_values = [];
          var y_values = [];
  
          // i add values to the arrays above here
  
          for(i = 0; i &lt; field_values.length; ++i) {
              dataToSubmit += "&amp;field_name="+field_values[i]+"&amp;xposition="+x_values[i]+"&amp;yposition="+y_values[i];
          }
  
          $.post($(this).attr('action'), dataToSubmit);   
      });
  </code></pre>
  
  <p>With my current code, when the form is submitted, it goes directly to the controller, and then the $.post call in my jquery code calls the same controller and passes xpostion and yposition in the data of the $.post call (which is what i want).</p>
  
  <p>How can i make it so only my form doesn't get submitted twice - once by hitting the controller as soon as the submit button is clicked and once from my $.post call?</p>
  
  <p>I thought :remote => true makes it so the form wouldn't call the controller directly?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If :remote => true sends an ajax request without me having to use jquery's $.post method to send the ajax request manually, how can i send extra variables along with the ajax request that automatically gets made by using :remote => true?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's a snippet from my server log</p>
  
  <pre><code>Started PUT "/applications/3" for 127.0.0.1 at 2013-01-18 00:06:19 -0600
  Processing by ApplicationsController#update as */*
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"3LOzuiL/PU6HypJ4OeN5H9yrX3Xyk0VT6XpcFYd1wY0=", "application"=&gt;{"application_field_attributes"=&gt;{"0"=&gt;{"id"=&gt;"8"}, "1"=&gt;{"id"=&gt;"9"}}}, "field_name"=&gt;"aaa", "xposition"=&gt;"0", "yposition"=&gt;"0", "id"=&gt;"3"}
    Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'localhost' LIMIT 1
    User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
    Application Load (0.4ms)  SELECT `applications`.* FROM `applications` WHERE `applications`.`id` = 3 LIMIT 1
  
  
  Started PUT "/applications/3" for 127.0.0.1 at 2013-01-18 00:06:19 -0600
  Processing by ApplicationsController#update as JS
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"3LOzuiL/PU6HypJ4OeN5H9yrX3Xyk0VT6XpcFYd1wY0=", "application"=&gt;{"application_field_attributes"=&gt;{"0"=&gt;{"id"=&gt;"8"}, "1"=&gt;{"id"=&gt;"9"}}}, "commit"=&gt;"Submit", "id"=&gt;"3"}
    Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'localhost' LIMIT 1
    User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
    Application Load (0.4ms)  SELECT `applications`.* FROM `applications` WHERE `applications`.`id` = 3 LIMIT 1
  </code></pre>