Question

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'
Was it helpful?

Solution

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"))%>")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top