1

I have the following code which I'd like to render via javascript (Kaminari pagination via AJAX):

<div id="paginator-jobs-top" class="text-center">
  <%= paginate @jobs, remote: true,
                       theme: 'twitter-bootstrap-3',
                  param_name: 'jobs',
                      params: { tab: 'jobs' },
            pagination_class: 'pagination-small pagination-centered' %>
</div>
<div id="jobs">
  <%= render partial: 'jobs/job_panel', collection: @jobs, as: :job %>
</div>
<div id="paginator-jobs-bottom" class="text-center">
  <%= paginate @jobs, remote: true,
                       theme: 'twitter-bootstrap-3',
                  param_name: 'jobs',
                      params: { tab: 'jobs' },
            pagination_class: 'pagination-small pagination-centered' %>
</div>

And I was trying with this js.erb code but its not working at all:

$('#paginator-jobs-top').html('<%= escape_javascript(paginate(@jobs,
                                                          remote: true, 
                                                           theme: "twitter-bootstrap-3",            
                                                          param_name: "jobs",   
                                                              params: { tab: "jobs" },
                 pagination_class: "pagination-small pagination-centered").to_s) %>');

$('#jobs').html('<%= escape_javascript(render partial: "jobs/job_panel", 
                                           collection: @jobs, 
                                                   as: :job) %>');

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!

The error I get from above is:

ArgumentError - wrong number of arguments (0 for 1)

When I try it with a slightly different line in the js.erb snippet (without ', job: j') I get:

NameError - undefined local variable or method `job' for #<#<Class:0x

**** Based on on Blelump's suggestion below I changed my code to this ***

<div id="jobs">
  <%= render partial: "jobs/job_panel", collection: @jobs, as: :job %>
</div>

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:

$('#jobs').html('<%= escape_javascript(render partial: "jobs/job_panel", 
                                           collection: @jobs, 
                                                   as: :job) %>');

The error is from Kaminari again:

NoMethodError - undefined method `current_page' for nil:NilClass:
  kaminari (0.16.1) lib/kaminari/helpers/action_view_extension.rb:18:in `paginate'
2
  • Do you get any error? Commented Oct 27, 2014 at 16:04
  • One error I get is: NameError - undefined local variable or method `job' for #<#<Class:0x0000010a2d4208>:0x00000108af5330> A different error with another attempt: ArgumentError - wrong number of arguments (0 for 1) Commented Oct 27, 2014 at 16:07

1 Answer 1

2

Try:

$('#jobs').html('<%=raw @jobs.collect {|j| escape_javascript(render :partial => "jobs/job_panel", :locals => {:job => j}) }.join("") %>');

or simpler, rendering a collection

$('#jobs').html('<%=raw escape_javascript(render :partial => "jobs/job_panel", :collection => @jobs, :as => :job) %>');
Sign up to request clarification or add additional context in comments.

8 Comments

Better but still not working, I've edited my original question to incorporate your info.
It looks like it comes from <%= paginate @elements %> as stated here: github.com/amatsuda/kaminari/blob/master/lib/kaminari/helpers/… . Did you provided corrent variable name?
Yes, I mean when I turn off the 'remote: true' option and run it, the pagination works fine both with my original way of looping and your suggestion of rendering a collection partial. All is ok. But as soon as I turn on 'remote: true' and transfer that snippet into show.js.erb it starts acting up. I will add more code... maybe its something else I'm doing.
Within your controller action, are you using request.xhr? or respond_to ?
No but you can use Kaminari without respond_to .js in my experience. In the controller I just call: @jobs = Kaminari.paginate_array(@array).page(params[:jobs]).per(5)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.