没有宝石的分页Next,Previous,Name.order(:id).limit(10).offset(0)的按钮 [英] Paginate without a gem Next, Previous, buttons for Name.order(:id).limit(10).offset(0)
问题描述
一个多星期以来,我一直在寻找解决问题的方法.我的一项作业由于没有下一个/上一个功能而失去了10分,并且时间用完了.我仍然想弄清楚这一点.
I've been searching for a solution to my problem for over a week now. I have an assignment that I lost 10 pts on due to no next/prev functionality and ran out of time. I still want to figure this out though.
我用rails generate scaffold Ripple name:string message:text url:string
创建了一个简短的单页站点,该站点显示了最近显示的10条帖子的索引(名称,消息,created_on,link_to"show").我仍然必须创建下一个,上一个,最新的,最旧的链接,以显示下10个,上一个10 ....的结果.我的代码.
I have Created a short single page site with rails generate scaffold Ripple name:string message:text url:string
that shows an index of 10 most recent posts displays (name, message, created_on, link_to "show"). I still have to create a next, previous, newest, oldest links in view to show next 10, prev 10.... results. My code.
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
<table>
<thead>
<tr>
<th>Name</th>
<th>Message</th>
<th>Posted</th>
<th>Show Ripple</th>
</tr>
</thead>
<tbody>
<% @ripples.each do |ripple| %>
<tr>
<td><%= link_name_to_url ripple %></td>
<td><%= truncate(ripple.message, length: 50) %></td>
<td><%= ripple.created_at.strftime("%B %d, %Y %l:%M %P") %></td>
<td><%= button_to 'Show', ripple, :method => "get" %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div id = "nav">
<button><%= link_to 'Newest' %></button>
<button><%= link_to 'Previous 10 Ripples' %></button>
<button><%= link_to "Next 10 Ripples" %></button>
<button><%= link_to 'Oldest' %></button>
<button><%= link_to 'New Ripple', new_ripple_path, class: "button", method: :get %></button>
</div>
而且我尝试在Model中调用方法,但在下一个和上一个上一直出现undefined method "next" for #<Class:0xb4eabd0c>
错误.
And I've tried calling methods in Model but keep getting undefined method "next" for #<Class:0xb4eabd0c>
error on next and previous.
app\models\ripple.rb
class Ripple < ActiveRecord::Base
default_scope -> {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实现下一个和上一个,也许使用@page
来跟踪我在ActiveRecord中的位置.也许像
How would I implement next and previous using the order().limit().offset and maybe use @page
to keep track of where I'm at in the ActiveRecord. Maybe something like
def next_button
@page -= 1
end
我可以用两种方式调用索引"<%= link_to Next 10" next_button %>
:
that I can call in index "<%= link_to Next 10" next_button %>
either way I'm out of ideas that might work.
感谢您的帮助.
推荐答案
这里有几件事.首先,控制器方法应与匹配路线一起使用.这是一个请求周期,您单击应用程序上的按钮,向服务器发出请求,然后向服务器发出信息.
A few things here. Firstly, controller methods should be used with matching routes. It's a request cycle where you click a button on your app, it makes a request to your server, then your server response with information.
以这种方式将其作为helper_method
放置时,您的next_button
方法将不起作用.为了使您的控制器正常工作,您应该具有与您的控制器方法匹配的路由.在命令行中执行rake routes
.您需要在route.rb
文件
Your next_button
method is not going to work when you put it as a helper_method
this way. In order to make your controller work, you should have routes that match to your controller method. Do a rake routes
in your command line. You need to see something like this in your route.rb
file
get 'ripple/next', 'ripple#next'
有关routes
的更多信息: http://guides.rubyonrails. org/routing.html#controller-namespaces-and-routing
在您的控制器中,您可以拥有
And in your controller, you could have
def next
page = params[:page]
params[:page] += 1
@ripples = Ripple.find_ripples_on_page(page, PAGE_SIZE)
render :index
end
然后在erb视图中,您应该访问"此特定路由,而不是调用该方法.
Then in your erb view, your should be 'visiting' this particular route, not calling the method.
第二,您不能依赖模型中的类实例变量.相反,您应该将其放在会话中或作为查询字符串的一部分.正如上面的控制器代码一样,我将其放在会话中,这是一个本地存储.有关session
的更多信息: http://guides.rubyonrails.org/action_controller_overview.html#session
Secondly, you can not rely on class instance variable in your model. Instead, you should put it in your session or as part of a query string. As above controller code, I put it in session, which is a local storage. More about 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
.
Side note: helper_method
in controller is for retrieving information not giving command to the controller. All this macro does is use class_eval
and 'copy' the method you have in controller into the renderer
.
如果可以使用宝石,请使用will_paginate
.
If you can use a gem, use will_paginate
.
http://guides.rubyonrails.org/routing.html#控制器命名空间和路由
这篇关于没有宝石的分页Next,Previous,Name.order(:id).limit(10).offset(0)的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!