简体   繁体   English

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

[英]Rails: Will_paginate does not work properly after AJAX call

Sorry about the repetition, I could not find the answers to my questions after a lot of searches. 很抱歉重复一次,经过大量搜索,我找不到我的问题的答案。 The will_paginate does not work after the Ajax call. Ajax调用之后,will_paginate不起作用。

  1. After delete action, It does not display the pages numbers at all (it simply returns all results). 删除操作后,它根本不显示页码(仅返回所有结果)。
  2. After search action, the page number does not update, it shows more pages than what should actually be there. 执行搜索操作后,页码不会更新,它显示的页面数超过实际应显示的页面数。

Here is the controller code. 这是控制器代码。

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

Here is the index view: 这是索引视图:

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

Here is the partial view of the table: 这是表的部分视图:

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

Found the solution by myself. 自己找到解决方案。

add <%= will_paginate @cities %> into partial file <%= will_paginate @cities %>到部分文件中

and change @cities = City.all to 并将@cities = City.all更改为

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

because will_paginate does not expect an array of objects. 因为will_paginate不需要对象array

write in your controller 写在您的控制器中

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

in your view folder create index.js.erb and write 在您的视图文件夹中创建index.js.erb并编写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM