没有gem的Paginate Next,Previous,Name.order(:id).limit(10).offset(0)的按钮

我一直在寻找解决我问题的方法一个多星期了。 由于没有下一个/上一个function而没有时间,我有一个我失去10分的任务。 我仍然想弄清楚这一点。

我创建了一个简短的单页网站,其中包含rails generate scaffold Ripple name:string message:text url:string ,显示最近发布的10个post的索引(名称,消息,created_on,link_to“show”)。 我仍然需要创建下一个,之前的,最新的,最旧的链接,以显示下一个10,前10 ….结果。 我的代码。

app\controllers\ripple_controller.rb

 class RipplesController < ApplicationController before_action :set_ripple, only: [:show, :update] before_action :restrict_destroy_edit, only: [:edit, :destroy] before_filter :set_page helper_method :link_name_to_url, :next_button, :previous_button, :newest_button, :oldest_button, :is_next_page_available, :is_previous_page_available RIPPLES_PER_PAGE = 10 def index @ripples = Ripple.order(:id).limit(RIPPLES_PER_PAGE).offset(@page * RIPPLES_PER_PAGE) end #All my show, new, destroy, edit, create .... def next_button end def previous_button end def newest_button end def oldest_button end def is_next_page_available? end def is_previous_page_available? end def set_page @page = 5 end private ... 

\app\views\ripples.html.erb

 
Name Message Posted Show Ripple
"get" %>

我已经尝试在模型中调用方法,但在下一个和上一个的undefined method "next" for #错误中继续使用undefined method "next" for #

app\models\ripple.rb

 class Ripple  {order(created_at: :desc)} validates :name, :message, presence: true validates :url, allow_blank: true, format: { with: URI::regexp(%w(http https)), message: "Must be a url starting with http:// or https://" } def next Ripple.order(:id).limit(10).offset((@page - 1) * 10) end def previous Ripple.order(:id).limit(10).offset((@page + 1) * 10) end end 

我如何使用order()。limit()。offset来实现next和previous,并且可以使用@page来跟踪我在ActiveRecord中的位置。 也许是这样的

 def next_button @page -= 1 end 

我可以在索引"调用,无论哪种方式,我都没有可能有效的想法。

谢谢你的帮助。

这里有一些事情。 首先,控制器方法应该与匹配的路由一起使用。 这是一个请求周期,您单击应用程序上的按钮,它向您的服务器发出请求,然后您的服务器响应信息。

当你把它作为helper_method这种方式时,你的next_button方法不会起作用。 为了使控制器工作,您应该拥有与您的控制器方法匹配的路由。 在命令行中执行rake routes 。 您需要在route.rb文件中看到类似的route.rb

 get 'ripple/next', 'ripple#next' 

有关routes更多信息: http : //guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

在你的控制器中,你可以拥有

 def next page = params[:page] params[:page] += 1 @ripples = Ripple.find_ripples_on_page(page, PAGE_SIZE) render :index end 

然后在你的erb视图中,你应该“访问”这个特定的路线,而不是调用方法。

其次,您不能依赖模型中的类实例变量。 相反,您应该将它放在会话中或作为查询字符串的一部分。 如上面的控制器代码,我把它放在会话中,这是一个本地存储。 有关session更多信息: http : //guides.rubyonrails.org/action_controller_overview.html#session

最后,你的模型。

 def find_ripples_on_page(page, PAGE_SIZE) Ripple.where( ... ) # you can figure this out, depending on if you want to rank by updated_at or other filter end 

附注 :控制器中的helper_method用于检索不向控制器发出命令的信息。 所有这个宏都使用class_eval并将控制器中的方法“复制”到renderer

如果你可以使用gem,请使用will_paginate

http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

这个怎么样?

 class RipplesController < ApplicationController before_filter :set_page, only: [:index] RIPPLES_PER_PAGE = 10 def index @ripples = Ripple.order(:id).limit(RIPPLES_PER_PAGE).offset(@page * RIPPLES_PER_PAGE) end private def set_page @page = params[:page].to_i || 0 end end 

然后在视图链接中

  

所有这些都将通过RipplesController#index方法驱动,并将使用类似/ripples?page=1/ripples?page=2等URL。

这些link_to调用将生成与GET变量的链接,以便您可以在params哈希中访问它们并使用它们来驱动返回的结果。