title: Using jQuery to load a Rails form into a table
body: <p>This is something that has killed my project dead for a few days now.<br>
  I have a standard table of values; with a skeleton something like this:</p>
  
  <pre><code>&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Stuff!&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
  </code></pre>
  
  <p>I am trying to load an inline Rails 3 form into the <code>tbody</code> using jQuery to let the user add new values to the table. This is done with a button which gets the new action of my controller, which in turn calls new.js.erb, which simply prepends the form's rendered partial into the table (<code>$("table tbody").prepend('&lt;%= escape_javascript(render 'form') %&gt;');</code>).</p>
  
  <p>The form partial itself goes something like this:</p>
  
  <pre><code>&lt;tr&gt;
    &lt;td&gt;
      &lt;%= form_for @model, :remote =&gt; true do |f| %&gt;
    &lt;/td&gt;
    &lt;td&gt;
      &lt;%= f.text_field :column %&gt;
    &lt;/td&gt;
    &lt;td&gt;
      &lt;%= f.submit "Add" %&gt;
      &lt;% end %&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Here's where the confusion begins: in Webkit browsers and IE this works as expected! The resulting HTML goes something like this:</p>
  
  <pre><code>&lt;table&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;form&gt;
          &lt;/form&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input type="submit"&gt;
        &lt;td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;The list of values&lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Of course, the fact that the form begins and ends in the same cell, before the inputs begin, should raise a red flag that something is going wrong. However, the form still submits and works perfectly otherwise.</p>
  
  <p>In Firefox, this is not the case. The form renders something more like this (shown is just the stuff inside the prepended row since everything else is the same):</p>
  
  <pre><code>&lt;td&gt;
    &lt;form&gt;
    &lt;/form&gt;
    &lt;td&gt;
      &lt;input&gt;
    &lt;/td&gt;
    &lt;td&gt;
      &lt;input type="submit"&gt;
    &lt;td&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>The difference here is that unlike the Webkit/IE browsers, Firefox throws everything into the <code>form</code> tag's cell. Also, the form does not work at all - it does not even submit. No requests even get sent out.</p>
  
  <p>Now, from some of my research I learned that there is probably a huge mistake I am making here and the difference comes from the fact that browsers try to render whatever I've left out in different ways. From playing around with it, I've figured out that this has little to do with how Rails forms interact with tables - if you just render the partial inside a table the HTML is identical between browsers (the form tag still closes before the other inputs begin, though, which may mean that I'm putting &lt;% end %> in the wrong place!). Something is happening wrong when I am prepending the rendered partial using jQuery: perhaps the partial renders wrong due to not having enclosing table tags and is then inserted into the table, causing the problems.</p>
  
  <p>Really, I have been stumped. I have looked for this far and wide and while there are some tangentially related questions none seem to really hit on my issue.</p>
  
  <p>I am expecting that I've made some very simple mistake - but what is it?</p>
  
  <p>Any help would be GREATLY appreciated! I hope I've explained the problem as clearly as possible.</p>