1

抱歉重复了,经过大量搜索后,我找不到问题的答案。调用后 will_paginate 不起作用Ajax

  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')) %>');
4

2 回答 2

1

自己找到了解决方案。

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

并更改@cities = City.all

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

因为will_paginate不期望一个array对象。

于 2012-10-29T23:09:00.863 回答
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 %>')
于 2012-10-29T05:52:03.110 回答