1

I am using the Will paginate gem with Ruby on Rails (4.2) and to render the pages I am using Ajax. I am using the will paginate helper to do it. And it is making the request for the pages correctly and receiving the response as well but not rendering the next HTML page. Can anyone tell me what is missing in it?

Am using :

<%= ajax_will_paginate @itmes, :params => { :my_excluded_param => nil } %>

to make pagination links.

Here is my pagination helper:

module WillPaginateHelper

  class WillPaginateAjaxLinkRenderer < WillPaginate::ActionView::LinkRenderer

    def prepare(collection, options, template)

      options[:params] ||= {}

      options[:params]["_"] = nil

      super(collection, options, template)

    end

    protected

    def link(text, target, attributes = {})

      if target.is_a? Fixnum

        attributes[:rel] = rel_value(target)

        target = url(target)

      end

      ajax_call = "$.ajax({url: '#{target}', dataType: 'script'});"

      @template.link_to_function(text.to_s.html_safe, ajax_call, attributes)

    end

  end

  def ajax_will_paginate(collection, options = {})

    will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateAjaxLinkRenderer))

  end

end

And my controller function is:

  def index

    @items = Item.paginate(:per_page => 5, :page => params[:page])

  end

I added the code index.js.erb

and now the terminal log is: and now the terminal log is:

Started GET "/items?page=2&_=1399700653418" for 127.0.0.1 at 2014-05-10 11:14:16 +0530
Processing by ItemsController#index as JS
  Parameters: {"page"=>"2", "_"=>"1399700653418"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
  Rendered items/index.js.erb (12.7ms)
Completed 500 Internal Server Error in 20ms

ActionView::Template::Error (Missing partial items/items, application/items with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
  * "/var/www/selectcom/app/views"
  * "/home/randhir/.rvm/gems/ruby-2.1.0/gems/kaminari-0.15.1/app/views"
  * "/home/randhir/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4/app/views"
):
    1: <%= "$('body').html('#{escape_javascript(render 'items')}');".html_safe %>
  app/views/items/index.js.erb:1:in `_app_views_items_index_js_erb__4572043876272285855_69867408217600'
Flip
  • 6,233
  • 7
  • 46
  • 75
Randhir
  • 735
  • 4
  • 11
  • 22
  • is it sending next page number on request ? – Dave May 10 '14 at 05:13
  • yes it is sending the page number.. – Randhir May 10 '14 at 05:22
  • please tell me if know something am missing.. – Randhir May 10 '14 at 05:38
  • Here is the terminal log: Started GET "/items?page=2&_=1399700457110" for 127.0.0.1 at 2014-05-10 11:11:08 +0530 Processing by ItemsController#index as JS Parameters: {"page"=>"2", "_"=>"1399700457110"} User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1 Rendered items/index.js.erb (0.5ms) Completed 200 OK in 10ms (Views: 6.7ms | ActiveRecord: 0.6ms) – Randhir May 10 '14 at 05:41
  • I added the code index.js.erb <%= "$('body').html('#{escape_javascript(render 'items')}');".html_safe %> – Randhir May 10 '14 at 05:44
  • it is searching for partial named items in folder named items. Do u have it,, if so update its contents clearly. – Dave May 10 '14 at 05:50
  • no I don't have any file named items in items folder. – Randhir May 10 '14 at 05:52
  • that's the problem :) ... refer answer by akshay vishnoi in this link, he has clearly written code http://stackoverflow.com/questions/13623953/how-to-implement-ajax-pagination-with-will-paginate-gem – Dave May 10 '14 at 05:54
  • in your items folder create a partial file name ( _items.html.erb ). this will solve missing template problem. – Dave May 10 '14 at 05:55
  • Hello Dave I have a problem regarding ajax will pagination. Can you help me? – Randhir May 17 '14 at 10:04
  • yeah,, post question link – Dave May 17 '14 at 10:06
  • Here is my question link: http://stackoverflow.com/questions/23710109/will-ajax-will-pagination-is-rendergin-the-page-twice – Randhir May 17 '14 at 10:15

1 Answers1

1

you did not create a partial file named items to be rendered on ajax request. Try as below.

app/controllers/items_controller.rb

def index
  @items = Item.paginate(:per_page => 5, :page => params[:page])
end

views/items/index.html.erb

<div class = "sort_paginate_ajax"><%= render 'items' %></div>

views/items/_items.html.erb

<% @items.each do |item| %>
# your code
 <%= item.name %>
<% end %>
<%= ajax_will_paginate @items %>

views/items/index.js.erb

$('.sort_paginate_ajax').html("<%= escape_javascript(render("items"))%>")
Dave
  • 4,376
  • 3
  • 24
  • 37
  • it is working fine but after rendering one page when again I clicked on next it starts refreshing the page? – Randhir May 10 '14 at 06:13
  • are you using <%= will_paginate @items %> in yours _items.html.erb ? if so , it will refresh page. because request will render html view. – Dave May 10 '14 at 06:16
  • yes am using <%= will_paginate @items %> in my _items.html.erb then how can I render the page without reloading? – Randhir May 10 '14 at 06:18
  • try your code like <%= ajax_will_paginate @items %> to make ajax request and let me know – Dave May 10 '14 at 06:18
  • Thanks very much Dave for you help. It is perfectly working now. – Randhir May 10 '14 at 06:27
  • Dave can you do little more favour for me? I used table sorting on my html page but when it loaded the next page using will paginate it removes sorting icons from table headers and also disable the sorting functionality. – Randhir May 10 '14 at 06:47
  • yup, where are you stuck ? – Dave May 10 '14 at 06:47
  • cannot answer without checking code, you have to create a new question, and mention your problem with all relevant code aligned neatly. – Dave May 10 '14 at 06:57