我正在使用jquery.pageless来实现无限滚动。
向下滚动时会出现错误:GET http://localhost:3000/search?page=2 500 (Internal Server Error)
,有时为GET http://localhost:3000/search?page=2 404 (Page Not Found)
search_controller.rb
require 'resque'
require 'will_paginate/array'
class SearchController < ApplicationController
def index
@blog = Blog.where(:name => params[:name])
if(@blog.nil?)
Resque.enqueue(HardWorker, params[:name])
end
search
end
def search
@search = Post.paginate(:per_page => 10, :page => params[:page], :order => ('created_at DESC'))
if request.xhr?
sleep(3)
render :partial => 'search/post'
end
end
end
search.html.erb
<%= render(:partial => "post", :collection => @search) %>
<%= will_paginate @search %>
<%= pageless(@search.total_pages, search_path) %>
_post.html.erb
<div style="margin-bottom:3em">
<h2><%= post.name %></h2>
</div>
模型/ Post.rb
class Post
include Mongoid::Document
include Mongoid::Pagination
store_in collection: "post", database: "post"
field :_id
field :blog_id, type: String
field :name
end
我在rails服务器上实现了一些东西:
Started GET "/search?page=2" for 127.0.0.1 at 2013-09-05 13:06:04 +0100
Processing by SearchController#search as HTML
Parameters: {"page"=>"2"}
MOPED: 127.0.0.1:27017 COMMAND database=post command=:count=>"post", :query=>{}} (0.0000ms)
MOPED: 127.0.0.1:27017 QUERY database=post collection=post selector={} flags=[:slave_ok] limit=10 skip=10 batch_size=nil fields=nil (1.0009ms)
MOPED: 127.0.0.1:27017 KILL_CURSORS cursor_ids=[112995992853088] (0.9999ms)
Rendered search/_post.html.erb (4.0ms)
Completed 500 Internal Server Error in 3012ms
ActionView::Template::Error (undefined method `[]'for nil:NilClass):
1: <div style="margin-bottom:3em">
2: <h2><%= post[:name] %></h2>
3: </div>
app/views/search/_post.html.erb:2:in _app_views_search__post_html_erb___963015535_21281256
app/controllers/search_controller.rb:20:in search
这是第一次显示前10个结果。但是当我向下滚动500内部服务器错误时出现。 有人可以帮我解决这个问题吗?谢谢!