Periodically Call Remote and Will_Paginate Problem

1 min read

I was using will_paginate and periodically_call_remote to update a table in a webpage periodically but it gave me a lot of trouble than I thought, when I used this I was about to leave office and bang a problem came. The problem will raise if you are using will_paginate for your pagination and updating the table or div ( or what ever you are using) periodically with periodically_call_remote. It was already 6pm so I didn’t payed that much attention to it at that time. I came up with two solutions later and thought I would blog here the simple solution first.

So what exactly the problem is?
I will show you my code that caused the problem first and will explain the reason for not working and one of the solution to solve this problem.
I have an index page with a html table and pagination. So my index action in the controller looks like this
[source language=”Ruby”]
def index
@books = Book.paginate :page => params[:page], :per_page => 5,
:order => “created_at DESC”
end
[/source]
Lets go the view and see the code of index.rhtml and partial _book_list.rhtml files
index.rhtml
[source language=”html”]

<%= render :partial => ‘book_list’%>


<%= periodically_call_remote :url => { :action => ‘periodically_update’ }, :frequency => ’60’ %>
[/source]
_book_list partial.rhtml
[source language=”html”]

Book nameAuthorCost
<%= book.book_name %> <%= book.author_name %> <%= book.cost %>

[/source]
So the action of periodically_update will be simple
[source language=”Ruby”]
def periodically_update
@books = Book.paginate :page => params[:page], :per_page => 5,
:order => “created_at DESC”
render :update do |page|
page.replace_html ‘book_list’, :partial => ‘book_list’
end
end
[/source]
Now this code has a problem whenever you periodically update the table It will get the new pagination links from periodically_update action so your pagination links will be changed and those links wont work.
http://localhost:3000/book/periodically_update?page=2 (but the correct link it should show is http://localhost:3000/book/index?page=2) Since there is no periodically_update.rhtml and so this link wont work.
The simple solution to resolve this problem is to merge the actions (index and periodcally_remote) under one action. But if you don’t use format.html and format.js It will cause another problem ( I hope you know what the problem is or you can try it).
[source language=”ruby”]
def index
@books = Book.paginate :page => params[:page], :per_page => 5,
:order => “created_at DESC”
respond_to do |format|
format.html
format.js {
render :update do |page|
page.replace_html ‘book_list’, :partial => ‘book_list’
end
}
end
end
[/source]
Now your index action will check whether it is a javascript call or html call, and based on that it will render the appropriate page content.
This problem has many solutions, if you have any other solutions please share those by adding comments to this blog.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *