title: Rails 4 render collection in partial via escape_javascript
body: <p>I have the following code which I'd like to render via javascript (Kaminari pagination via AJAX):</p>
  
  <pre><code>&lt;div id="paginator-jobs-top" class="text-center"&gt;
    &lt;%= paginate @jobs, remote: true,
                         theme: 'twitter-bootstrap-3',
                    param_name: 'jobs',
                        params: { tab: 'jobs' },
              pagination_class: 'pagination-small pagination-centered' %&gt;
  &lt;/div&gt;
  &lt;div id="jobs"&gt;
    &lt;%= render partial: 'jobs/job_panel', collection: @jobs, as: :job %&gt;
  &lt;/div&gt;
  &lt;div id="paginator-jobs-bottom" class="text-center"&gt;
    &lt;%= paginate @jobs, remote: true,
                         theme: 'twitter-bootstrap-3',
                    param_name: 'jobs',
                        params: { tab: 'jobs' },
              pagination_class: 'pagination-small pagination-centered' %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And I was trying with this js.erb code but its not working at all:</p>
  
  <pre><code>$('#paginator-jobs-top').html('&lt;%= escape_javascript(paginate(@jobs,
                                                            remote: true, 
                                                             theme: "twitter-bootstrap-3",            
                                                            param_name: "jobs",   
                                                                params: { tab: "jobs" },
                   pagination_class: "pagination-small pagination-centered").to_s) %&gt;');
  
  $('#jobs').html('&lt;%= escape_javascript(render partial: "jobs/job_panel", 
                                             collection: @jobs, 
                                                     as: :job) %&gt;');
  </code></pre>
  
  <p>I tried other variations but none of them worked. If anyone knows how to render the above code in a js.erb method I'd love to hear!</p>
  
  <p>The error I get from above is:</p>
  
  <pre><code>ArgumentError - wrong number of arguments (0 for 1)
  </code></pre>
  
  <p>When I try it with a slightly different line in the js.erb snippet (without ', job: j') I get:</p>
  
  <pre><code>NameError - undefined local variable or method `job' for #&lt;#&lt;Class:0x
  </code></pre>
  
  <p>**** Based on on Blelump's suggestion below I changed my code to this ***</p>
  
  <pre><code>&lt;div id="jobs"&gt;
    &lt;%= render partial: "jobs/job_panel", collection: @jobs, as: :job %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Which works great without AJAX (this is actually the correct way to render collection partials in Rails, my mistake before). However when I adjust the js.erb file to this I still get an error:</p>
  
  <pre><code>$('#jobs').html('&lt;%= escape_javascript(render partial: "jobs/job_panel", 
                                             collection: @jobs, 
                                                     as: :job) %&gt;');
  </code></pre>
  
  <p>The error is from Kaminari again:</p>
  
  <pre><code>NoMethodError - undefined method `current_page' for nil:NilClass:
    kaminari (0.16.1) lib/kaminari/helpers/action_view_extension.rb:18:in `paginate'
  </code></pre>