无尽的滚动不工作 [英] endless scrolling does not work
问题描述
更新:我的Github存储库 https://github.com/Marc585/smartforce2
Update: My Github Repository https://github.com/Marc585/smartforce2
我刚刚完成了onemonthrails教程。在最后一章它关于无尽滚动。我tripple检查我的代码,但它只是不工作。我不会得到一个错误或任何东西。它只是不做任何事情。我正在建立一个pinterest克隆,我滚动到底部后,应该加载下一页的pin。
i just finished the onemonthrails tutorial. at the last chapter its about endless scrolling. i tripple checked my code but it just doesn't work. I don't get an error or anything. It just doesn't do anything. I'm building a pinterest clone and after i scroll to the bottom it should load the next page of pins.
这是我的pins.js.coffee
This is my pins.js.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
$('#pins').imagesLoaded ->
$('#pins').masonry itemSelector: ".box"
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page a').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
# What to do at the bottom of the page
$('.pagination').text("Fetching more pins...")
$.getScript(url)
$(window).scroll()
这是我的index.js.erb
This is my index.js.erb
$boxes = $('<%= j render(@pins) %>')
$('#pins').append( $boxes ).imagesLoaded( function(){
$('#pins').masonry( 'reload');
});
<% if @pins.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@pins) %>');
<% else %>
$('.pagination').remove();
<% end %>
这是我的pin控制器:
This is my pins controller:
class PinsController < ApplicationController
before_filter :authenticate_user!, except: [:index]
# GET /pins
# GET /pins.json
def index
@pins = Pin.order("created_at desc").page(params[:page]).per_page(20)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @pins }
format.js
end
end
# GET /pins/1
# GET /pins/1.json
def show
@pin = Pin.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
end
end
# GET /pins/new
# GET /pins/new.json
def new
@pin = current_user.pins.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @pin }
end
end
# GET /pins/1/edit
def edit
@pin = current_user.pins.find(params[:id])
end
# POST /pins
# POST /pins.json
def create
@pin = current_user.pins.new(params[:pin])
respond_to do |format|
if @pin.save
format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
format.json { render json: @pin, status: :created, location: @pin }
else
format.html { render action: "new" }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# PUT /pins/1
# PUT /pins/1.json
def update
@pin = current_user.pins.find(params[:id])
respond_to do |format|
if @pin.update_attributes(params[:pin])
format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pins/1
# DELETE /pins/1.json
def destroy
@pin = current_user.pins.find(params[:id])
@pin.destroy
respond_to do |format|
format.html { redirect_to pins_url }
format.json { head :no_content }
end
end
end
我的开发者工具的错误消息
https://www.dropbox.com/s/odqknmw7np1f4cv/Bildschirmfoto%202013-08-31%20um%2014.34.40.png
Error Message from my developer tools https://www.dropbox.com/s/odqknmw7np1f4cv/Bildschirmfoto%202013-08-31%20um%2014.34.40.png
推荐答案
在您的节目中缺少
动作在pins_controller.rb。这是从onemonth rails注释
format.js in your show action in the pins_controller.rb. This is from the onemonth rails notes
注意:你必须创建一个类似的show.js.erb并添加format.js选项给你的用户#show action
Note: You'll have to create an analogous show.js.erb and add the format.js option to your users#show action to get endless scroll to work on users' profile pages as well
def show
@pin = Pin.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
format.js
end
end
您的show.js.erb应该如下所示:
Your show.js.erb should look like this
boxes = $('<%= j render(@pins) %>')
$('#pins').append( $boxes ).imagesLoaded( function(){
$('#pins').masonry( 'reload');
});
<% if @pins.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@pins) %>');
<% else %>
$('.pagination').remove();
<% end %>
这篇关于无尽的滚动不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!