我正在使用Kaminari为我的应用添加分页,但是当我点击" next"转到第二页,我没有看到下一组内容。
我的观点看起来像这样
<div class="span1">
<% @guestbooks.each do |g| %>
<br/>
<h4><%= g.name %>, <%= g.created_at %><br/></h4>
<%= g.message %><br/>
<p>-----------------------------------------------------------------------------------------------------------------------</p>
<% end %>
</div>
显示数据的第一页上的我的网址是http://localhost:3000/guestbooks/new
数据未显示在http://localhost:3000/guestbooks/new?page=2
上我无法确定原因。
这是我的控制器
class GuestbooksController < ApplicationController
def index
@guestbooks = Guestbook.all.limit(5).page(params[:page])
end
def new
@guestbook = Guestbook.new
@guestbooks = Guestbook.all.limit(5).page(params[:page])
@guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(1)
end
def create
@guestbook = Guestbook.new(guestbook_params)
# @guestbooks = Guestbook.all.limit(1).page(params[:page])
if @guestbook.save
flash.now[:notice] = "Thanks for taking the time to write us! We greatly appreciate it!"
render :new
else
flash.now[:notice] = "Your message failed to post, please try again"
render :new
end
end
private
def guestbook_params
params.require(:guestbook).permit(:name, :email, :message)
end
end
答案 0 :(得分:2)
如果我们再次通过kaminari做同样的事情我们不需要限制或分页
试试这个:
@guestbooks= Guestbook.all
@guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(1)