Firstly, I have got an Ajax will_paginate working and it’s great!
/clients/index.html.erb - this displays the CRUD listing of my
clients. The Ajax pagination is added through a partial and the div
container “results” is being reloaded.
I then added a form, with a text field and a search button, so i can
refine my search. I got this working with jQuery.put to refresh the
“results” div container. So when i type in a few letters, i go off and
call my search_results action in my clients_controller which calls the
will_paginate magic. My problem is that when the “results” container
is updated with the results, my will_paginate page links are being
changed from
2
to
2
Can somebody please help! I have no idea why this is happening and
have spent days on this!
Please also feel free to critique my code as I am a noobie
public/javascript/application.js
jQuery(document).ready(function() {
jQuery(“#search_form”).submit(function() {
jQuery.post(jQuery(“#search_form”).attr(‘action’)
, jQuery(this).serialize()
, function(data)
{jQuery(“#results”).html(data);}
, “html”
);
return false;
})
})
/clients/index.html.erb
<% form_tag clients_path+‘/search_results’, :id => ‘search_form’ do %>
<% content_tag :label do %>
Search term (first name):
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :id => 'fubar' %>
<% end %>
<% end %>
model/client.rb
def self.search(search, page)
# convert to lowercase for search
search = String(search).downcase
paginate :per_page => 2,
:page => page,
:conditions => ['LOWER(first_name) like ?', "%#{search}
%"],
:order => ‘first_name’
end
/controller/clients_controller.rb
def search_results
# Saving search term to be used in the partial search template.
@clients = Client.search(params[:search], params[:page])
render :partial => 'listing'
end