3

私は Rails を初めて使用し、ネストされたリソースで will_paginate を機能させるのに大きな問題を抱えています。

Statement と Invoice の 2 つのモデルがあります。will_paginate は Statement で動作していますが、Invoice では動作しません。ばかげたことをしていることはわかっていますが、それを理解できず、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) %> のように呼び出してみます。

これをいじってみると、最も一般的なエラー メッセージは次のとおりです。

問題が何であるか、またはそれを修正する方法についての手がかりがありません。ヘルプとガイダンスをありがとう!

4

1 に答える 1

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|
  %>
于 2009-09-25T07:13:18.637 に答える