ROR 请求一个操作来获取一系列项目
我如何创建一个接受参数来限制搜索结果的操作?这将超出我的帖子模型
Posts_controller:
def getOneThrougTen
params[firstNum] params[secondNum]
某种类型的返回范围?
结束
How would I create an action that takes in parameters to limit search results? This would be out of my Posts model
Posts_controller:
def getOneThrougTen
params[firstNum] params[secondNum]
return range of some sort?
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我并不完全清楚你想要实现什么,但如果它以块的形式显示搜索结果,我会建议你使用 will_paginate 宝石!
It's not entirely clear to me what you want to achieve, but if it is displaying search results in chunks, I'll advice you to use the will_paginate gem!
如果您正在寻找分页类型代码,您应该查看 Rails 3 中 Arel 和 ActiveRecord 提供的新功能。这将允许您执行以下操作:
或
等等。非常强大的东西。
If you're looking for pagination type code, you should look at the new capabilities provided with Arel and ActiveRecord in Rails 3. That will allow you to do the following:
or
etc. etc. Very powerful stuff.
一旦您了解了活动记录如何模仿 sql 调用的基础知识,传递参数就变得直观了。例如,这就是您如何根据特定列的范围从 Post 模型创建 @posts 集合、对结果进行排序并限制返回的数量。
您可以观看脚本/服务器输出以查看实际的 SQL,以帮助了解正在发生的情况以及为什么某些事情没有按计划进行。
要实际发送并读取您的参数..
在控制器中,您可以读取视图发送的参数并将它们分配给变量。例如你可以这样做。
实际上将它们从视图发送到控制器是不同的,具体取决于您是否使用完全安静的控制器(例如在 Railsscaffoldind 中生成的控制器)。如果您想要的只是将它们发送到非静态方法(例如“getDateRangeandPassItOn”),那么在您看来,您会执行类似的操作。
Once you learn the basics of how active record mimics sql calls passing the parameters becomes intuitive. For example this is how you would create a collection of @posts from your Post model based on a range from a particular column, order the results and limit the number returned.
You can watch the script/server output to see the actual SQL to help understand what is going on and why something doesn't go as planned.
To actually send and read your params..
In your controller you can read params sent by the view and assign them to variables. For example you could do.
To actually send them from the view to your controller is different depending on if you are using a fully restful controller such as the one generated in rails scaffoldind. If all you want is to send them into a non restful method such as "getDateRangeandPassItOn" you would do something like this in your view.