我在这个 paginate_by_sql 调用中做错了什么?
原谅我,伙计们;我仍然是一个完全 RoR 新手。
我正在尝试使用 will_paginate gem 为我的 Rails 中的搜索结果添加分页应用程序。到目前为止,它运行良好。然而,我遇到了障碍。
我有一个包含数千条记录的“产品”表,我希望这些记录可以完全浏览。这在最基本的场景中很容易。我的控制器中有这个:
page = params[:page] || 1
@products = Product.paginate :page => page, :order => 'symbol'
我想要做的是添加一个参数“字母”,以便只有符号以给定字母开头的产品才会被分页和显示。
这就是我尝试过的:
letter = params[:letter] || 'A'
page = params[:page] || 1
@products = Product.paginate_by_sql
['select * from products where symbol like ?', letter + '*'],
:page => page
:order => 'symbol'
但是使用此代码后,页面将无法加载。我在日志文件中看到了这一点,但对我来说它实际上看起来并不像一个错误:
Processing ProductsController#index (for 127.0.0.1 at 2010-09-15 10:03:22) [GET] [4;36;1mProduct Load (9.0ms)[0m [0;1mselect * from products where symbol like 'A*' LIMIT 50 OFFSET 0[0m Rendering template within layouts/main Rendering products/index Completed in 21ms (View: 4, DB: 9) | 200 OK [http://localhost/products]
我意识到我可能遗漏了一些明显的东西,或者不包含解决此问题所需的信息。有人愿意引导我走向正确的方向,或者让我知道我应该提供哪些其他信息吗?
Forgive me, guys; I'm still a complete RoR newbie.
I'm trying to use the will_paginate gem to add pagination to search results in my Rails app. Thus far it's been working great. However, I've hit a roadblock.
I have a "products" table with thousands of records that I want to be completely browsable. This is easy in the most basic scenario. I've got this in my controller:
page = params[:page] || 1
@products = Product.paginate :page => page, :order => 'symbol'
What I want to do is add a parameter, "letter," so that only products whose symbols start with the given letter are paginated and displayed.
This is what I tried:
letter = params[:letter] || 'A'
page = params[:page] || 1
@products = Product.paginate_by_sql
['select * from products where symbol like ?', letter + '*'],
:page => page
:order => 'symbol'
But with this code in place, the page won't load. I see this in the log file, but it doesn't actually look like an error to me:
Processing ProductsController#index (for 127.0.0.1 at 2010-09-15 10:03:22) [GET] [4;36;1mProduct Load (9.0ms)[0m [0;1mselect * from products where symbol like 'A*' LIMIT 50 OFFSET 0[0m Rendering template within layouts/main Rendering products/index Completed in 21ms (View: 4, DB: 9) | 200 OK [http://localhost/products]
I realize I'm probably missing something obvious, or else not including the information that would be needed to solve this problem. Would anyone care to steer me in the right direction, or let me know what other info I should provide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定你使用的是什么数据库,但我相信你的 SQL 是错误的。这不应该是
letter + '%'
而不是letter + '*'
吗?% 通常是 SQL 通配符。
Not sure what database you are using, but I believe your SQL is wrong. Shouldn't this be
letter + '%'
notletter + '*'
?% is generally the SQL wildcard.