2013-08-27 53 views
0

更新:我Github上庫https://github.com/Marc585/smartforce2無盡的滾動不起作用

我剛剛完成了onemonthrails教程。在最後一章講述無盡的滾動。我tripple檢查我的代碼,但它不起作用。我沒有收到任何錯誤或任何信息。它只是沒有做任何事情。我正在構建一個pinterest克隆,在我滾動到底部之後,它應該加載下一頁的引腳。

這是我的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的

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

這是我銷控制器:從

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

+0

貴'scroll'回調得到調用? 'url'你期望的是什麼?你是否檢查過「if」中的其他值? –

+0

@ muistooshort不幸的是,我不知道你剛剛說的是什麼意思。我是ROR初學者。我剛剛添加了我的github存儲庫。如果你可以看看會很棒。謝謝 –

+0

那麼,有時間去學習一點調試。嘗試向CoffeeScript添加一些'console.log'調用來查看你的回調是否被調用。並檢查瀏覽器開發人員工具中的請求,以查看服務器和瀏覽器對對方說的話。 –

回答

0

你缺少

format.js在你的show動作pins_controller.rb。這是從一個月的軌跡筆記

注意:您將不得不創建一個類似的show.js.erb並將format.js選項添加到您的用戶#show action以獲得無限滾動以處理用戶的配置文件頁面以及

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應該是這樣的

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