我在哪里对此过滤对象进行“.paginate”调用?

时间:2013-10-22 14:34:56

标签: ruby-on-rails pagination ruby-on-rails-4 will-paginate

我想对我的索引页面上列出的约会进行分页,但我不确定在哪里放置分页消息。以下是我在控制器中实现索引方法的方法:

def index
    if !current_user.nil?
      filter = []
      @my_apps = Appointment.joins(:client).where("user_id = ?", current_user.id)

      if !params[:paid].blank?
        paid = params[:paid]
        filter << ["copay_received = '#{paid.to_s}'"]
      end

      if !params[:effective_on].blank?
        junk = params[:effective_on]
        filter << ["clients.effective_on >= '#{junk.to_s}'"]
      end 

      @unpaids = @my_apps.where(filter.join(" AND "))  
    else
      redirect_to '/signin'
    end
  end
end

我尝试将.paginate(page: params[:page])附加到Appointment对象,在where子句之后和@my_apps的where子句之后,但没有运气。

请让我知道我做错了什么。

1 个答案:

答案 0 :(得分:2)

设置@my_apps后尝试执行此操作。

例如......

@my_apps = Appointment.joins(:client).where("user_id = ?", current_user.id)
@my_apps = @my_apps.paginate(page: params[:page])

我遇到了和你一样的问题,但这解决了我的问题。