2009-09-23 5 views
3

레일스에 익숙하지 않아 중첩 된 리소스로 작업하기 위해 will_paginate를 얻는 데 큰 어려움을 겪고 있습니다.will_paginate를 Rails에서 중첩 된 리소스와 함께 사용하는 방법은 무엇입니까?

저는 Statement와 Invoice라는 두 가지 모델이 있습니다. will_paginate가 Statement에서 작업하고 있지만 송장에서 처리 할 수 ​​없습니다. 내가 어리석은 짓을하는 것을 알고 있지만, 알아낼 수 없으며 Google에서 찾은 사례가 나를 위해 효과가 없을 것입니다.

statement.rb 
class Statement < ActiveRecord::Base 
    has_many :invoices 

    def self.search(search, page) 
    paginate :per_page => 19, :page => page, 
     :conditions => ['company like ?', "%#{search}%"], 
     :order => 'date_due DESC, company, supplier' 
    end 
end 

statements_controller.rb <irrelevant code clipped for readability> 
def index #taken from the RAILSCAST 51, will_paginate podcast 
    @statements = Statement.search(params[:search], params[:page]) 
end 

I call this in the view like so, and it works: 
    <%= will_paginate @statements %> 

그러나 나는 송장을 위해 작동하도록하는 방법을 알아낼 수 없습니다 :

invoice.rb 
class Invoice < ActiveRecord::Base 
    belongs_to :statement 

    def self.search(search, page) 
    paginate :per_page => 19, :page => page, 
     :conditions => ['company like ?', "%#{search}%"], 
     :order => 'employee' 
    end 
end 

invoices_controller.rb 
class InvoicesController < ApplicationController 

    before_filter :find_statement 


    #TODO I can't get will_paginate to work w a nested resource 
    def index #taken from the RAILSCAST 51, will_paginate podcast 
     @invoices = Invoice.search(params[:search], params[:page]) 
    end 

def find_statement 
    @statement_id = params[:statement_id] 
    return(redirect_to(statements_url)) unless @statement_id 
    @statement = Statement.find(@statement_id) 
    end 
end 

그리고 다음과 같이 호출하려고 : <퍼센트 = will_paginate (@invoices를) %>

가장 일반적인 오류 메시지는 다음과 같습니다. "@statements 변수가 비어 있습니다. will_paginate의 컬렉션 개체를 전달하는 것을 잊었습니까?"

나는 문제가 무엇인지, 또는 문제를 해결하는 방법에 대한 단서가 없습니다. 어떤 도움과지도 주셔서 감사합니다!

답변

5

는 해결 -

나는 송장이 같은 성명의 컨트롤러에 페이지 매김 이동 :

def show 
    @statement = Statement.find(params[:id]) 

    #TODO move the :per_page stuff out to a constant 
    @invoices = @statement.invoices.paginate :per_page => 10, 
    :page => params[:page], 
    :order => 'created_at DESC' 


respond_to do |format| 
    format.html # show.html.erb 
    format.xml { render :xml => @statement } 
end 
end 

및 코드 가독성>

<div id="pagination"> 
    <%= will_paginate @invoices %> 
    </div> 
    <table> 
    <%# @statement.invoices.each do |invoice| - 
    shows all invoices with no pagination, 
    use @invoices instead%> 
    <% 
    @invoices.each do |invoice| 
    %> 
에 대한 손질 (이 같은 관점에서 호출