Using will_paginate with active AJAX lookup using jQuery in Rails

I use will_paginate to write successfully through writes. I also use real-time AJAX using jQuery to update my div. There are no problems so far. The problem I have is trying to break into pages with real-time search results. I just get "Page loading ..." without updating the div. Am I missing something? I believe that I might need to somehow bind the click event of the page links using jQuery.

# index.html.erb

  <form id="searchform" accept-charset="utf-8" method="get" action="/search">
    Search: <input id="search" name="search" type="text" autocomplete="off" title="Search location, company, description..." />      
   <%= image_tag("spinner.gif", :id => "spinner", :style =>"display: none;" ) %>  
  </form>

# JobsController#search

def search
    if params[:search].nil?
      @jobs = Job.paginate :page => params[:page], :order => "created_at desc"
    elsif params[:search] and request.xhr?
      @jobs = Job.search params[:search], params[:page]
    end
    render :partial => "jobs", :layout => false, :locals => { :jobs => @jobs }
  end

# Job#search

def self.search(search, page)
    logger.debug "Job.paginate #{search}, #{page}"
    paginate :per_page => @@per_page, :page => page,
             :conditions => ["description LIKE ? or title LIKE ? or company LIKE ?", 
               "%#{search}%", "%#{search}%", "%#{search}%"], 
               :order => 'created_at DESC'
  end

# search.js

$(document).ready(function(){

  $("#search").keyup(function() {
    $("#spinner").show(); // show the spinner
    var form = $(this).parents("form"); // grab the form wrapping the search bar.
    var url = form.attr("action"); // grab the URL from the form action value.
    var formData = form.serialize(); // grab the data in the form
    $.get(url, formData, function(html) { // perform an AJAX get, the trailing function is what happens on successful get.
      $("#spinner").hide(); // hide the spinner
      $("#jobs").html(html); // replace the "results" div with the result of action taken
    });
  });

});
+3
source share
1 answer

, /search - log/development.log. , :page, :

page = params[:page] || 1
0

Source: https://habr.com/ru/post/1738808/


All Articles