2012-10-29 111 views
1

對於重複,很抱歉,經過大量搜索後我找不到問題的答案。在撥打Ajax後,will_paginate不起作用。Rails:在AJAX調用後Will_paginate無法正常工作

  1. 刪除操作後,它根本不顯示頁碼(它只是返回所有結果)。
  2. 搜索操作後,頁碼不會更新,它顯示的頁數比實際存在的頁數多。

這裏是控制器代碼。

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> 

下面是表的局部視圖:

<div class="row"> 
<h1>Enter City descriptions or <%= link_to "Search Cities", cities_path %></h1> 
<%= form_for @city, html: {multipart: true} do |f| %> 

<%= f.label :city_name %> 
<%= f.text_field :city_name %> 

<%= f.label :city_description %> 
<%= f.text_field :city_description %> 

<%= f.label :city_population_in_units_of_millions %> 
<%= f.text_field :city_population %> 
<label></label> 

<%= f.file_field :image%> 

<label></label> 
<%= f.submit "Create new City", :class => "btn"%> 
<% end %> 
</div> 

Finally two js.erb codes associated with index and delete actions: 

index.js.erb: 

$('#table').html('<%= escape_javascript(render('table')) %>'); 

destroy.js.erb: 

$('#table').html('<%= escape_javascript(render('table')) %>'); 

回答

1

自己找到解決方案。

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

並改變@cities = City.all

@cities = City.all.paginate(頁面:PARAMS [:頁])

因爲will_paginate不期望一個對象的array

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 %>') 
+0

感謝您的回答。我試了一下,和以前一樣。阿賈克斯的作品。問題仍然存在,因爲will_paginate根本沒有更新........ –