Forum: Ruby on Rails Ajax will_paginate + jQuery.put changes will_paginate href

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E9f9febdefbb834326c9433a27c8d835?d=identicon&s=25 obiwan chinobi (dmht81)
on 2009-07-04 19:13
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
<a rel="next" href="/clients?page=2">2</a>
to
<a rel="next" href="/clients/search_results?page=2">2</a>

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 %>

<div id="results">
        <%= render :partial => 'listing' %>
</div>

----------------------------------------------------------------
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
32d80da41830a6e6c1bb3eb977537e3e?d=identicon&s=25 Alessandro R. (alessandro_r)
on 2011-07-20 16:39
Same problem here.
The links are generated from the request address.
If i delete an element from the list (ajax way) the links in the
pagination are wrong. Don't know how to solve. Please help!
This topic is locked and can not be replied to.