Rails:AJAX调用后,Will_paginate无法正常工作

时间:2012-10-29 05:43:15

标签: ruby-on-rails ajax will-paginate

很抱歉重复,经过大量搜索,我无法找到问题的答案。 {_ 1}}调用后,will_paginate无效。

  1. 删除操作后,它根本不显示页码(只返回所有结果)。
  2. 搜索操作后,页码不会更新,显示的页面数量应超过实际应用的页数。
  3. 这是控制器代码。

    Ajax

    这是索引视图:

    class CitiesController < ApplicationController
    
     def index
         @cities = City.get_cities(params[:city_name],params[:city_population]).paginate(:page => params[:page])
      respond_to do |format|
        format.html 
        format.js
      end
    
     end
    
     def create
          @city = City.new(params[:city])
    
      if @city.save
           flash[:success] = "City information saved successfully"
    
           redirect_to cities_path
        else
            render 'index'
        end
     end
    
    
    def new
        @city = City.new
    end
    
    
     def destroy
       @city = City.find(params[:id]).destroy
       @city.destroy
       @cities = City.all
       respond_to do |format|
          format.html {redirect_to cities_path}
          format.js
        end
      end
    
     end
    

    以下是该表的局部视图:

     <div class="row">
    <h1>Search Cities or <%= link_to "Create New City", new_city_path %></h1>
    <h3>99 random city information are generated in the database </h2>
    <h3>Simply type any letter or city population between 0 and 10 to filter out</h3>
    <%= form_tag "/cities/index", method: :get, remote: true do %>
      <%= label_tag(:q, "City Name:") %>
      <%= text_field_tag 'city_name' %>
      <%= label_tag(:q, "City Population greater than, in units of 1 million:") %>
      <%= text_field_tag 'city_population' %>
      <label></label>
      <%= button_tag("Search", :class => "btn") %>
    <% end %>
    <% flash.each do |key, value| %>
       <div class="alert alert-<%= key %>"><%= value %></div>
    <% end %>
    
    
    <div id='table' class="table table-striped">
        <%= render 'table' %> 
    </div>
    
    
    <%=will_paginate @cities %>
     </div>
    

2 个答案:

答案 0 :(得分:1)

自己找到解决方案。

<%= will_paginate @cities %>添加到部分文件

并将@cities = City.all更改为

  

@cities = City.all.paginate(页面:params [:page])

因为will_paginate不期望array个对象。

答案 1 :(得分:0)

写入您的控制器

def index
  @cities = City.get_cities(params[:city_name],params[:city_population]).paginate(:page => params[:page])
  respond_to do |format|
   format.html 
   format.js  
  end 
end

在您的视图文件夹中创建index.js.erb并写入

$('id or class').html('<%=escape_javascript render :partial => which part you update %>')