Ruby on rails 如何使用will_paginate和Rails中的嵌套资源?
我是Rails新手,在让will_paginate使用嵌套资源时遇到了很大的困难 我有两种型号,对账单和发票。will_paginate正在处理对账单,但我无法处理发票。我知道我在做一些傻事,但我想不出来,我在谷歌上找到的例子对我来说也不管用Ruby on rails 如何使用will_paginate和Rails中的嵌套资源?,ruby-on-rails,will-paginate,nested-resources,Ruby On Rails,Will Paginate,Nested Resources,我是Rails新手,在让will_paginate使用嵌套资源时遇到了很大的困难 我有两种型号,对账单和发票。will_paginate正在处理对账单,但我无法处理发票。我知道我在做一些傻事,但我想不出来,我在谷歌上找到的例子对我来说也不管用 statement.rb class Statement < ActiveRecord::Base has_many :invoices def self.search(search, page) paginate :per_pag
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 %>
statement.rb
class语句19页,:每页=>19页,
:conditions=>['companylike',“%#{search}%”,
:订单=>“日期\到期说明、公司、供应商”
结束
结束
语句\u controller.rb
def索引取自RAILSCAST 51,将用于播客
@语句=语句.search(参数[:search],参数[:page])
结束
我在视图中这样称呼它,它是有效的:
但我不知道如何让它用于发票:
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
invoice.rb
类发票19页,:每页=>19页,
:conditions=>['companylike',“%#{search}%”,
:order=>employee'
结束
结束
发票控制器.rb
类InvoicesController
我试着这样称呼它:
当我处理这个问题时,最常见的错误消息是:
“@statements变量似乎为空。您是否忘记为will_paginate传递集合对象?”
我不知道问题是什么,也不知道如何解决。谢谢你的帮助和指导 已解决-
我将发票分页移动到报表的控制器中,如下所示:
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|
%>