不带 gem 的分页 Next、Previous、Name.order(:id).limit(10).offset(0) 按钮 [英] Paginate without a gem Next, Previous, buttons for Name.order(:id).limit(10).offset(0)

查看:18
本文介绍了不带 gem 的分页 Next、Previous、Name.order(:id).limit(10).offset(0) 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个多星期以来,我一直在寻找解决问题的方法.我有一个作业,由于没有下一个/上一个功能,我失去了 10 分,而且时间不够了.不过我还是想弄清楚.

我用 rails generate scaffold Ripple name:string message:text url:string 创建了一个简短的单页站点,它显示了 10 个最新帖子的索引(名称、消息、created_on、link_to展示").我仍然需要创建下一个、上一个、最新、最旧的链接以显示下一个 10、上一个 10.... 结果.我的代码.

appcontrollers ipple_controller.rb

class RipplesController <应用控制器before_action :set_ripple, 仅: [:show, :update]before_action :restrict_destroy_edit, 仅: [:edit, :destroy]before_filter :set_pagehelper_method :link_name_to_url, :next_button, :previous_button, :newest_button, :oldest_button, :is_next_page_available, :is_previous_page_availableRIPPLES_PER_PAGE = 10定义索引@ripples = Ripple.order(:id).limit(RIPPLES_PER_PAGE).offset(@page * RIPPLES_PER_PAGE)结尾#我所有的节目,新建,销毁,编辑,创建....定义 next_button结尾定义前一个按钮结尾定义最新按钮结尾定义最旧的按钮结尾def is_next_page_available?结尾def is_previous_page_available?结尾def set_page@页面 = 5结尾私人的...

appviews ipples.html.erb

<头><tr><th>姓名</th><第>消息</th><th>已发布</th><th>显示波纹</th></tr></thead><% @ripples.each 做 |ripple|%><tr><td><%=link_name_to_url 涟漪%></td><td><%= truncate(ripple.message, length: 50) %</td><td><%=ripple.created_at.strftime("%B %d, %Y %l:%M %P") %></td><td><%= button_to 'Show', 涟漪, :method =>得到"%></td></tr><%结束%></tbody>
<br><div id = "导航"><button><%= link_to 'Newest' %></button><button><%= link_to '前 10 个涟漪' %></button><button><%= link_to下一个 10 个涟漪"%></button><button><%= link_to 'Oldest' %></button><button><%= link_to 'New Ripple', new_ripple_path, class: "button", method: :get %></button>

而且我已经尝试在模型中调用方法,但在下一个和上一个错误上不断收到 undefined method "next" for #<Class:0xb4eabd0c> 错误.

appmodels ipple.rb

class Ripple <ActiveRecord::Basedefault_scope ->{订单(created_at::desc)}验证:名称,:消息,存在:真验证:url,allow_blank:true,格式:{与:URI::regexp(%w(http https)),消息:必须是以 http://或 https://开头的网址"}下一个定义Ripple.order(:id).limit(10).offset((@page - 1) * 10)结尾定义前一个Ripple.order(:id).limit(10).offset((@page + 1) * 10)结尾结尾

我将如何使用 order().limit().offset 实现下一个和上一个,并且可能使用 @page 来跟踪我在 ActiveRecord 中的位置.也许像

def next_button@页面-= 1结尾

我可以调用索引 "<%= link_to Next 10" next_button %> 无论哪种方式,我都没有可能可行的想法.

感谢您的帮助.

解决方案

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

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

中看到类似的内容

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

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

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

def next页 = 参数[:页]参数[:页面] += 1@ripples = Ripple.find_ripples_on_page(page, PAGE_SIZE)渲染:索引结尾

然后在您的 erb 视图中,您应该访问"这条特定路线,而不是调用该方法.

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

最后,你的模型.

def find_ripples_on_page(page, PAGE_SIZE)Ripple.where( ... ) # 你可以弄清楚这一点,这取决于你是想通过 updated_at 还是其他过滤器进行排名结尾

旁注:控制器中的helper_method 用于检索信息,而不是向控制器发出命令.这个宏所做的就是使用 class_eval 并将您在控制器中的方法复制"到 renderer 中.

如果您可以使用 gem,请使用 will_paginate.

http://guides.rubyonrails.org/routing.html#控制器命名空间和路由

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.

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.

appcontrollers ipple_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
...

appviews ipples.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>

And I've tried calling methods in Model but keep getting undefined method "next" for #<Class:0xb4eabd0c> error on next and previous.

appmodels ipple.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

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

that I can call in index "<%= link_to Next 10" next_button %> either way I'm out of ideas that might work.

Thanks for any help.

解决方案

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.

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'

More about 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

Then in your erb view, your should be 'visiting' this particular route, not calling the method.

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

Finally, your model.

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

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.

If you can use a gem, use will_paginate.

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

这篇关于不带 gem 的分页 Next、Previous、Name.order(:id).limit(10).offset(0) 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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