|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (7) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-17
问题描述:已经从电影数据库中随机取了20部movies,如何仅仅这20部movies进行分页,显示在页面上
controller中: def test @movies = Movie.find_by_sql("select * from movies order by rand() limit 20") @movies.paginate(:per_page => 5, :page => page) end rthml中: <%= will_paginate @movies %> 请教一下以上代码应该如何修改 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-04-17
paginate对find函数进行包装了
试试这么写: @movies=Movie.paginate_by_sql "select * from movies order by rand() limit 20", :per_page => 5, :page => page 所有 find_xxx 都可以直接用 paginate_xxx ..笔误,已修改 |
|
| 返回顶楼 | |
|
时间:2008-04-17
7thbyte 写道 paginate对find函数进行包装了
试试这么写: @movies.paginate_by_sql "select * from movies order by rand() limit 20", :per_page => 5, :page => page 所有 find_xxx 都可以直接用 paginate_xxx 这样做的话,出现错误 undefined local variable or method `page' 怎么解决 |
|
| 返回顶楼 | |
|
时间:2008-04-17
.. page 不是你自己传的参数么,传一个int值进去就行了。。
例如 :page=>params[:page] |
|
| 返回顶楼 | |
|
时间:2008-04-17
page是他里面包含的参数,
|
|
| 返回顶楼 | |
|
时间:2008-04-17
点击换页按钮之后还是会穿回来的。index的时候你可以直接设定你的page.回来的时候你就用params[:page]
|
|
| 返回顶楼 | |
|
时间:2008-04-18
shawnccx 写道 7thbyte 写道 paginate对find函数进行包装了
试试这么写: @movies.paginate_by_sql "select * from movies order by rand() limit 20", :per_page => 5, :page => page 所有 find_xxx 都可以直接用 paginate_xxx 这样做的话,出现错误 undefined local variable or method `page' 怎么解决 又试了一下 错误报告:You have a nil object when you didn't expect it! The error occurred while evaluating nil.paginate_by_sql 是不是好像paginate_xxx语句不行?? |
|
| 返回顶楼 | |
|
时间:2008-04-18
shawnccx 写道 又试了一下 错误报告:You have a nil object when you didn't expect it! The error occurred while evaluating nil.paginate_by_sql 是不是好像paginate_xxx语句不行?? 很不好意思,是我的笔误: controller里面只需要这样一句就可以查询并分页。 @movies=Movie.paginate_by_sql "select * from movies order by rand() limit 20", :per_page => 5, :page => params[:page] 然后view上访问@movies对象即可 |
|
| 返回顶楼 | |
|
时间:2008-04-18
7thbyte 写道 shawnccx 写道 又试了一下 错误报告:You have a nil object when you didn't expect it! The error occurred while evaluating nil.paginate_by_sql 是不是好像paginate_xxx语句不行?? 很不好意思,是我的笔误: controller里面只需要这样一句就可以查询并分页。 @movies=Movie.paginate_by_sql "select * from movies order by rand() limit 20", :per_page => 5, :page => params[:page] 然后view上访问@movies对象即可 Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 5' at line 1: select * from movies order by rand() limit 20 LIMIT 0, 5 之前的问题我也意识到了,但现在由于分页为每页5个单位,导致sql语句有错误. |
|
| 返回顶楼 | |
|
时间:2008-04-18
shawnccx 写道 Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 5' at line 1: select * from movies order by rand() limit 20 LIMIT 0, 5 之前的问题我也意识到了,但现在由于分页为每页5个单位,导致sql语句有错误. ummm... 先随机拿出20条再分页么。。 看来是一个子查询的问题。。 这么写吧... 子查询 as a 或者as xxx... @movies=Movie.paginate_by_sql "select * from (select * from movies order by rand() limit 20) as a", :per_page => 5, :page => params[:page] 我用控制台测试了一下应该没问题 |
|
| 返回顶楼 | |




