ruby-on-rails - 仅使用下一个按钮分页

标签 ruby-on-rails ruby pagination

我正在尝试制作下一个按钮,它应该每页只显示一条记录。我知道我们可以使用分页,但我只想有 Next 按钮。因此,我阅读了以下帖子并实现了它: 我的问题 Controller :

class QuestionsController < ApplicationController
  before_action :set_page, only: [:view]
  QUESTIONS_PER_PAGE = 1
  def view
    questions=[]
    @evaluation = Evaluation.pluck(:content).last
    @evaluation.each do |question|
      questions << question
    end
    @questions=Question.where(:content => questions).limit(QUESTIONS_PER_PAGE).offset(@page*QUESTIONS_PER_PAGE)
    #@questions=Question.where(:content => questions).paginate(page: params[:page],per_page: 1)
  end
  private
    def set_page
       @page = params[:page] || 0
    end

我的 view.html.erb 给出错误:没有将 Integer 隐式转换为 String

<h1>Self-Evaluation Exam</h1>

<table id="questions">
    <thead>
        <tr>
            <th>Content &nbsp; &nbsp; &nbsp;</th>
        </tr>
    </thead>
    <tbody>
        <% @questions.each do |question| %>
          <tr>
            <td><%= question.content %></td>
            <td><%= check_box_tag question.c1 %></td>
            <td><%= question.c1%></td>
            <td><%= check_box_tag question.c2 %></td>
            <td><%= question.c2%></td>
            <td><%= check_box_tag question.c3 %></td>
            <td><%= question.c3%></td>
            <td><%= check_box_tag question.c4 %></td>
            <td><%= question.c4%></td>
            <td><%= check_box_tag question.c5 %></td>
            <td><%= question.c5%></td>
          </tr>
        <% end %>
    </tbody>
</table>
<div id = "nav">
    <%= button_to 'Next',questions_path(page: @page + 1) %># this line gives error: no implicit conversion of Integer into String
</div>
<li><%= link_to 'Personal Evaluation',instructions_student_path %></li>

Paginate without a gem Next, Previous, buttons for Name.order(:id).limit(10).offset(0) 它显示第一页但是当我点击按钮时我得到错误 PS:当它在最后一条记录时,下一步按钮应该改为提交 谢谢

最佳答案

很简单,试试下面的方法

QUESTIONS_PER_PAGE = 1
@questions = Question.where(content: questions).offset(QUESTIONS_PER_PAGE * @page).limit(QUESTIONS_PER_PAGE)

private
def set_page
   @page = (params[:page] || 0).to_i
end

更改为您的 view.html.erbnext >><< previous

<div id="nav">
    <nav>
        <ul class="pager">
            <li class="previous <%= @page == 0 ? 'disabled' : '' %>">
                <%= link_to_if page > 0, "&larr; Previous".html_safe, questions_path(page: @page - 1) %>
            </li>
            <li class="next">
                <%= link_to "Next &rarr;".html_safe, questions_path(page: @page + 1) %>
            </li>
        </ul>
    </nav>
</div>

所以你的 controller看起来像这样

class QuestionsController < ApplicationController
    before_action :set_page, only: [:view]
    QUESTIONS_PER_PAGE = 1
    def view
        questions=[]
        @evaluation = Evaluation.pluck(:content).last
        @evaluation.each do |question|
          questions << question
        end
        @questions = Question.where(content: questions).offset(QUESTIONS_PER_PAGE * @page).limit(QUESTIONS_PER_PAGE)
    end

    private
    def set_page
       @page = (params[:page] || 0).to_i
    end
end

如果你只需要下一个按钮到你的 View ,那么只使用

<%= link_to "Next &rarr;".html_safe, questions_path(page: @page + 1) %>

有条件

<% if @page == @questions.count %>
    <button>Finished</button>
<% else %>
    <%= link_to "Next &rarr;".html_safe, questions_path(page: @page + 1) %>
<% end %>

关于ruby-on-rails - 仅使用下一个按钮分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49786496/

相关文章:

ruby-on-rails - Capybara:填写表格和 save_and_open_page

c - 为 Ruby 1.9.1 构建 dnssd gem 时缺少 htons

ruby-on-rails - Ruby/Rails - kaminari 未定义的方法分页错误

ruby-on-rails - 在没有数据库的情况下将数据存储在 Ruby on Rails 中

mysql - 如何找到现有的 ("parent") 记录并向其添加关联记录

ruby-on-rails - 为搜索选项制作 cookie?

c# - 在 C# 中针对没有 Skip 的 DocumentDB 进行分页

jquery - 使用 Jquery 附加动态分页 - 如何保存位置?

ruby-on-rails - 如何使用 Nokogiri 抓取非标准 HTML

ruby-on-rails - Belongs_to 无法正常工作