在 Rails 应用程序中添加选择功能

发布于 2024-12-10 05:30:28 字数 117 浏览 4 评论 0原文

我正在学习 ruby​​ on Rails。因此,我创建了一个示例应用程序,在其中显示一些内容。现在我想添加根据用户的选择缩小项目范围的功能,例如更改价格范围、选择类型等。

请推荐一些有帮助的文章/教程。

I am learning ruby on rails. So, I created a sample application in which I am displaying some stuffs. Now I want to add the functionality of narrowing down items depending upon user's selection like changing price ranges, selecting type and so on.

Please suggest some articles / tutorials which can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-12-17 05:30:28

通过模型查找器来完成此操作。

例如
http://railscasts.com/episodes/37-simple-search-form

http://railscasts.com/episodes/111-advanced-search-form

本教程是关于范围的...http://railscasts.com/episodes/111-advanced-search-form 但非常相关,实际上有一些最简单的例子。

在模型(而不是控制器)中进行搜索的最大收益之一是测试。您现在可以进行单元测试,例如,如果您使用 rspec,则可以在模型内进行 rspec 模型测试。

也许您想要动态范围?
动态作用域允许动态过滤和方法链接。例如:

Order.scoped_by_customer_id(12)
Order.scoped_by_customer_id(12).find(:all,
  :conditions => "status = 'open'")
Order.scoped_by_customer_id(12).scoped_by_status("open")

Do this through model finders.

e.g.
http://railscasts.com/episodes/37-simple-search-form

http://railscasts.com/episodes/111-advanced-search-form

This tutorial is about scopes... http://railscasts.com/episodes/111-advanced-search-form but is very relevant and actually has some of the simplest examples.

One of the biggest gains from doing the search in the model (as opposed to the controller) is testing. You can now have unit tests, e.g. rspec model tests if you use rspec, just within the model.

Perhaps you want dynamic scope?
Dynamic scopes allow filtering on the fly and method chaining. For example:

Order.scoped_by_customer_id(12)
Order.scoped_by_customer_id(12).find(:all,
  :conditions => "status = 'open'")
Order.scoped_by_customer_id(12).scoped_by_status("open")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文