title: Ajax Call using jQuery in Rails app
body: <p>I already have a form with normal HTTP post. Now, I want to rewrite this form usign jQuery Ajax request. Here is what I have:</p>
  
  <p><strong>register_user.html.erb</strong></p>
  
  <pre><code>&lt;%= form_tag :action=&gt; "register_user" %&gt;
                                 &lt;label for="user_id_1"&gt;user_id_1:&lt;/label&gt;&lt;br/&gt;
                                 &lt;%= text_field "user", "user_id_1", :size =&gt; 20 %&gt; 
                                 &lt;label for="user_id_2"&gt;user_id_2:&lt;/label&gt;&lt;br/&gt;
                                 &lt;%= text_field "user", "user_id_2", :size =&gt; 20 %&gt;
  &lt;%= submit_tag "Submit" %&gt;
  </code></pre>
  
  <p><strong>users_controller.rb</strong></p>
  
  <pre><code>def register_user
  
     #do something
  
  end
  </code></pre>
  
  <p>So, I was trying to follow <a href="http://railscasts.com/episodes/136-jquery" rel="nofollow noreferrer">this</a> tutorial, but was not able to follow the same instructions in my app. So any suggestions? How can I make jQuery Ajax call using the above form?</p>
  
  <p>AFAIK, the <strong>application.js</strong> (file included in register_user.html.rb's head) file will be</p>
  
  <pre><code>jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
  })
  
  jQuery.fn.submitWithAjax = function() {
    this.submit(function() {
      $.post(this.action, $(this).serialize(), null, "script");
      return false;
    })
    return this;
  };
  
  $(document).ready(function() {
    $("#new_review").submitWithAjax(); #WHAT SHOULD REPLACE #new_review?
  })
  </code></pre>
  
  <p>But, how can I associate the submit action to the form? I am new to JS, jQuery and Rails.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Rails 2.3.8</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <pre><code>&lt;%= form_tag :action=&gt; "register_user" , :html =&gt; {:id =&gt; "register_user" }%&gt;
  </code></pre>
  
  <ol>
  <li><p>The request does not go in the <code>jQuery.fn.submitWithAjax = function() { }</code> method.</p></li>
  <li><p>How can I display the <code>@final_value</code> data on the same page?</p></li>
  </ol>
  
  <p>For now, this is my code to display the value (in <code>register_user.html.erb</code>):</p>
  
  <pre><code>    &lt;%=
      if @final_value != nil
          "&lt;h2&gt;The Output is " + @final_value.to_s&lt;/h2&gt;" + 
      end
       %&gt;
  </code></pre>
  
  <p>Thanks for the awesome help guys!</p>