title: Passing ActionView::Helpers::FormBuilder to a partial
body: <p>I am atempting to dinamically create form elements given a certain AJAX request. </p>
  
  <p>This is my setup:</p>
  
  <p>View:</p>
  
  <pre><code>    &lt;%= link_to 'Next', check_unique_id_students_path, :remote =&gt; true %&gt;
  
      &lt;div id="guardian_student_details"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>def check_unique_id
      @student = Student.new
      @this_form = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
    end
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>jQuery("#guardian_student_details").html("&lt;%=escape_javascript(render :partial =&gt; "student_details", :locals =&gt; { :s =&gt; @this_form }) %&gt;");
  </code></pre>
  
  <p>Partial:</p>
  
  <pre><code>&lt;% puts s.text_field :first_name   %&gt;
  &lt;% puts s.field_helpers   %&gt;
  </code></pre>
  
  <p>For debugging purposes i placed the following lines at the very beginning of my partial:</p>
  
  <pre><code>&lt;% puts s.class.to_s %&gt;
  &lt;% puts s.object.to_s %&gt;
  </code></pre>
  
  <p>This prints out :</p>
  
  <pre><code>ActionView::Helpers::FormBuilder
  Student
  </code></pre>
  
  <p>This should work. However rails is giving the following error:</p>
  
  <pre><code>ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
  1: &lt;% puts s.class.to_s   %&gt;
  2: &lt;p&gt;
  3: &lt;%= s.text_field :first_name, :class =&gt; 'text_input is_empty' %&gt;
  4: &lt;%= s.label :first_name %&gt;&lt;strong&gt;*&lt;/strong&gt; 
  5: &lt;/p&gt;
  6: 
  </code></pre>
  
  <p>app/views/students/_student_details.html.erb:3:in <code>_app_views_students__student_details_html_erb__2485891544130782916_2214680440'
    app/views/students/check_unique_id.js.erb:2:in</code>_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'</p>
  
  <p>Which implies that "s" is NIL something I verified just 2 lines before. Does anybody have any ideas? i dont know if this has something to do with the "@template" variable initialized in the controller. Which i played around with and accepts practically anything and if printed is nil.
  Any help would be appreciated. Thanks</p>
  
  <p>Final note:</p>
  
  <p>I tried to implement this: <a href="https://stackoverflow.com/questions/4206057/ajax-update-of-accepts-nested-attributes-for-partials">AJAX update of accepts_nested_attributes_for partials</a></p>