我正在尝试在项目中使用AJAX无限滚动来实现will_paginate gem。尝试使用它时,我在JS控制台中收到以下错误。
[错误]无法加载资源:服务器响应状态为404(未找到)(过滤器,第0行)
[错误]无法加载资源:服务器响应状态为404(未找到)(过滤器,第0行)
我所知道的:
这是堆栈跟踪:
Started GET "/ajax-loader.gif" for ::1 at 2016-01-24 09:30:51 -0500
ActionController::RoutingError (No route matches [GET] "/ajax-loader.gif"):
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.1) lib/rails/engine.rb:518:in `call'
railties (4.2.1) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/static.rb:124:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/jon/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/jon/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/jon/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Rendered /Users/jon/.rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
Rendered /Users/jon/.rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.3ms)
Rendered /Users/jon/.rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (20.9ms)
Rendered /Users/jon/.rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
Rendered /Users/jon/.rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (134.3ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/_markup.html.erb (0.6ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/console.js.erb within layouts/javascript (56.4ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /Users/jon/.gem/ruby/2.2.2/gems/web-console-2.1.3/lib/web_console/templates/index.html.erb (129.3ms)
Started GET "/vehicles/filter?page=2&_=1453645847210" for ::1 at 2016-01-24 09:30:52 -0500
Processing by VehiclesController#show as JS
Parameters: {"page"=>"2", "_"=>"1453645847210", "id"=>"filter"}
Vehicle Load (0.2ms) SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 0]]
Rendered public/404.html (0.6ms)
Completed 404 Not Found in 55ms (Views: 52.2ms | ActiveRecord: 0.2ms)
index.js.erb的
$('#vehicles').append('<%= j render @vehicles %>');
<% if @vehicles.next_page %>
$('.pagination').replaceWith('<%= j will_paginate @vehicles %>');
<% else %>
$(window).off('scroll');
$('.pagination').remove();
<% end %>
pagination.js
$(document).ready(function () {
if ($('#infinite-scrolling').size() > 0) {
return $(window).on('scroll', function() {
var more_posts_url;
more_posts_url = $('.pagination a.next_page').attr('href');
if (more_posts_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60) {
$('.pagination').html('<img src="ajax-loader.gif" alt="Loading..." title="Loading..." />');
$.getScript(more_posts_url);
}
return;
});
}
});
的routes.rb
...
resources :vehicles do
collection do
post 'filter' => 'vehicles#index'
end
member do
get 're_edit' => 'vehicles#re_edit'
post 'resubmit' => 'vehicles#resubmit'
patch 'resubmit' => 'vehicles#resubmit'
end
end
...
vehicles_controller.rb
def index
scope = Vehicle.approved
scope = scope.filter_by_make(params[:makes]) if params[:makes].present?
scope = scope.filter_by_year(params[:years]) if params[:years].present?
@vehicles = scope.paginate(page: params[:page], per_page: 6).order('created_at DESC')
respond_to do |format|
format.html
format.js
end
end
index.html.erb
<section id="vehicles" class="content">
<% @vehicles.each do |vehicle| %>
<div>
<h2><%= link_to "#{vehicle.make} #{vehicle.model}", vehicle %></h2>
<%= image_tag(vehicle.primary_image.image, class: "img-responsive") if vehicle.primary_image %>
<p><%= vehicle.year %> <%= vehicle.make %> <%= vehicle.model %></p>
<%= link_to "View Details", vehicle, class: "details" %>
</div>
<% end %>
</section>
<div id="infinite-scrolling">
<%= will_paginate %>
</div>