轨道 3 和雷分页问题
好的,我决定在 Rails 3 项目中使用 Kaminari 进行分页。我关注了 RailsCasts http://railscasts.com/episodes/254-pagination- 的视频with-kaminari
一切都很顺利,直到运行服务器为止。
controllers/stories_controller.rb
def index
@stories = Story.all
@pages = Story.page(params[:page]).per(3)
@stories = Story.search(params[:search])
end
views/stories/index.html.erb
<%= paginate @pages %>
当我启动服务器时,相关索引页显示所有来自的故事数据库并呈现分页视图,显示 (1 2 Next > Last »)。为了使分页正常工作,我缺少什么?
Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari
All goes well up until the point or running the server.
controllers/stories_controller.rb
def index
@stories = Story.all
@pages = Story.page(params[:page]).per(3)
@stories = Story.search(params[:search])
end
views/stories/index.html.erb
<%= paginate @pages %>
When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last »). What am I missing to get the pagination working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我仍然无法理解你的代码。为什么将第一行中的 Story.all 分配给 @stories 并覆盖第三行中的变量?
无论如何,@stories 将显示“数据库中的所有故事”,因为您没有在 @stories 上调用分页方法 (
.per
)。分页链接将显示分页计数,因为您正在对 @page 变量调用per
方法并将其传递给帮助程序。我的意思是,您需要在将关系传递给
<%= paginate %>
帮助器之前对关系调用.per
。这很简单。
I still can not understand your code. Why do you assign Story.all to @stories in the 1st line and overwrite the variable in the 3rd line?
Anyways, @stories will display "all the stories from the DB" because you're not calling the pagination method (
.per
) on @stories. The pagination links will show you the paginated counts because you're callingper
method on @page variable and passing it to the helper.I mean, you need to call
.per
on the relation before passing it to<%= paginate %>
helper.It's quite simple.
我想您想从搜索中获得结果,对吧?
尝试
类似的事情:
在你看来
I guess you want to get results from your search, right?
Try
and something like:
in your view