如何在 Rails 中将 will_pagination 与嵌套资源一起使用? [英] How to use will_paginate with a nested resource in Rails?

查看:39
本文介绍了如何在 Rails 中将 will_pagination 与嵌套资源一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,我在让 will_paginate 使用嵌套资源时遇到了很大的麻烦.

我有两个模型,Statement 和 Invoice.will_paginate 正在处理 Statement,但我无法让它处理 Invoice.我知道我会做一些愚蠢的事情,但我想不通,而且我在 google 上找到的示例对我不起作用.

statement.rb类声明19, :page =>页,:条件=>['company like ?', "%#{search}%"],:订单=>'date_due DESC、公司、供应商'结尾结尾statements_controller.rb <为了可读性而剪裁的无关代码>def index #取自 RAILSCAST 51, will_paginate 播客@statements = Statement.search(params[:search], params[:page])结尾我在视图中这样称呼它,并且它有效:<%= will_paginate @statements %>

但我不知道如何让它用于发票:

invoice.rb类发票19, :page =>页,:条件=>['company like ?', "%#{search}%"],:订单=>'员工'结尾结尾invoices_controller.rb类 InvoicesController <应用控制器before_filter :find_statement#TODO 我无法让 will_paginate 在嵌套资源中工作def index #取自 RAILSCAST 51, will_paginate 播客@invoices = Invoice.search(params[:search], params[:page])结尾def find_statement@statement_id = params[:statement_id]return(redirect_to(statements_url)) 除非@statement_id@statement = Statement.find(@statement_id)结尾结尾

我试着这样称呼它:<%= will_paginate (@invoices) %>

最常见的错误消息是:@statements 变量似乎为空.您是否忘记为 will_paginate 传递集合对象?"

我不知道问题是什么,也不知道如何解决.感谢您的任何帮助和指导!

解决方案

已解决 -

我将发票分页移动到 Statement 的控制器中,如下所示:

def 显示@statement = Statement.find(params[:id])#TODO 将 :per_page 内容移到常量中@invoices = @statement.invoices.paginate :per_page =>10、:page =>参数[:页面],:订单=>'created_at DESC'response_to do |格式|format.html # show.html.erbformat.xml { 渲染:xml =>@陈述 }结尾结尾

并像这样在视图中调用它(为了可读性而修剪了代码>

 

<%= will_paginate @invoices %>

<表格><%# @statement.invoices.each do |invoice|——显示没有分页的所有发票,改用@invoices %><%@invoices.each 做 |发票|%>

I'm new to Rails, and I'm having major trouble getting will_paginate to work with a nested resource.

I have two models, Statement and Invoice. will_paginate is working on Statement, but I can't get it to work on Invoice. I know I'd doing something silly, but I can't figure it out and the examples I've found on google won't work for me.

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

But I can't figure out how to get it to work for Invoices:

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

And I try to call it like this: <%= will_paginate (@invoices) %>

The most common error message, as I play with this, is: "The @statements variable appears to be empty. Did you forget to pass the collection object for will_paginate?"

I don't have a clue what the problem is, or how to fix it. Thanks for any help and guidance!

解决方案

Solved -

I moved the invoices pagination into Statement's controller, like this:

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

and call it in the view like this (code trimmed for readability>

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

这篇关于如何在 Rails 中将 will_pagination 与嵌套资源一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆