gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:03 24 4
gpt4 key购买 nike

我正在尝试制作下一个按钮,它应该每页只显示一条记录。我知道我们可以使用分页,但我只想有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com