title: Rails 3 - Got "Undefined local variable or method `f' " when use ajax to execute remote javascript
body: <p>I have a question on this:
  I have a partial named <code>_return_item.html.erb</code>, so I want to render it repeatedly and dynamically.</p>
  
  <pre><code>&lt;%= f.fields_for :return_items, return_item do |item_form| %&gt;
    some_field_input_for_return_item
  &lt;%end%&gt;
  </code></pre>
  
  <p>The <code>index.html.erb</code> is like this:</p>
  
  <pre><code>&lt;%= form_for(@return_request) do |f| %&gt;
    &lt;div id="contact"&gt;
      &lt;!-- some_field_input_for_request_contact_info --&gt;
    &lt;/div&gt;
    &lt;div id="itemlist"&gt;
      &lt;!-- This part is working--&gt;
  
      &lt;% @return_request.return_items.each do |item| %&gt;
        &lt;%= render :partial =&gt; "return_items/return_items", :locals =&gt; {:f =&gt; f, :return_item =&gt; item}%&gt;
        &lt;!-- I_want_to_append_the_partial_here --&gt;
      &lt;%end%&gt;
  
    &lt;/div&gt;
    &lt;%= button_tag("Add New Item", :class=&gt;"addItem" )%&gt;
  &lt;%end%&gt;
  
  &lt;script&gt;
    $(document).ready( function () {
      $( '.addItem' ).click(function () {
        $.ajax({
          url: "return_requests/request_new_item",
          data: {
                 item_num: ($( '#itemlist' ).children().length)
                },
          dataType: "script"
        })
        return false;
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>The <code>request_new_item</code> controller is like this:</p>
  
  <pre><code>def request_new_item
    @return_item = ReturnItem.new 
  end
  </code></pre>
  
  <p>and here is <code>request_new_item.js.erb</code> :</p>
  
  <pre><code> $("#itemlist").append('&lt;%= j render :partial =&gt; "return_items/return_items", :locals =&gt; {:f =&gt; f, :return_item =&gt; item } %&gt;');
  </code></pre>
  
  <p>When I press the button <code>addItem</code>, I expect to generate a new partial, but actually, this doesn't work. The console provide an error:</p>
  
  <pre><code> ActionView::Template::Error (undefined local variable or method `f' for #&lt;#&lt;Class:0x007f5a302232b0&gt;:0x007f5a380afea8&gt;)
  </code></pre>
  
  <p>UPDATE:
  I forgot to post the model:
  <code>return_request.rb</code>:</p>
  
  <pre><code>class ReturnRequest &lt; ActiveRecord::Base
      has_many :return_items, :class_name=&gt;'ReturnItem', :foreign_key=&gt;'RMA_Nmb', :primary_key=&gt;'RMA_Nmb'
      accepts_nested_attributes_for :return_items
  end
  </code></pre>
  
  <p><code>return_item.rb</code>:</p>
  
  <pre><code>class ReturnItem &lt; ActiveRecord::Base
    belongs_to :return_request, :class_name=&gt;"ReturnRequest"
  end
  </code></pre>
  
  <p>So, could anybody explain me how this happen, and how can I fix this? Thanks</p>