Rails:列表和索引之间的区别

发布于 2024-09-06 22:24:08 字数 903 浏览 3 评论 0原文

我知道这是一个令人难以置信的菜鸟问题,但在谷歌上搜索了多种搜索词组合后,遗憾的是我仍然一无所知。当一个人不知道时,这些事情可能会很困难,而当一个人知道时,这些事情就会变得很明显。

我有半主页,新来的客户可以选择查看查询列表或执行一些其他操作。这不是主页,而是网站内的一种迷你“总机”。

七个标准的 RESTful Rails 控制器方法是(据我所知):

List # shows a list of records generated with .find(:all)
Show # shows details on one record
New # initiates new record
Create # saves and renders/redirects
Edit # finds and opens existing record for edit
Update # updates attributes
Delete # deletes record
  1. 当某些用户需要查看并非字面上的 .find(:all) 的选定记录“列表”时,应使用什么?鉴于我仍然需要一个列表函数来为我提供 .find(:all) 用于其他目的,这将如何工作?
  2. 我听说过 Rails 控制器中使用“索引”,但我不知道索引和列表之间的区别。
  3. 为了获得最佳实践和最佳设计,您会为迷你总机(以及其他中间页面,例如“关于我们”)使用什么控制器方法?

任何具体答案都会比 http://guides.rubyonrails.org/action_controller_overview 的链接更有用。 html 等。:) 非常感谢。

I appreciate this is an incredibly noob question, but having Googled multiple combinations of search terms I regretfully am still in the dark. These things can be difficult when one doesn't know and so obvious when one does.

I have semi-home page where incoming customers can choose to see a queried list or do a handful of other things. This isn't a home page but a sort of mini 'switchboard' within the site.

The seven standard RESTful Rails controller methods are (as I understand them):

List # shows a list of records generated with .find(:all)
Show # shows details on one record
New # initiates new record
Create # saves and renders/redirects
Edit # finds and opens existing record for edit
Update # updates attributes
Delete # deletes record
  1. What to use when some users need to see a selected 'list' of records that isn't literally .find(:all)? How would this work given I still need a list function that gives me .find(:all) for other purposes?
  2. I've heard of 'index' being used in Rails controllers, but I don't know the difference between index and list.
  3. For best practice and best design, what controller methods would you use for a mini-switchboard (and other intermediate pages such as 'About Us')?

Any specific answers would be a bit more useful than links to http://guides.rubyonrails.org/action_controller_overview.html etc. :) Thanks very much.

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

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

发布评论

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

评论(1

我们的影子 2024-09-13 22:24:08

首先,我认为重要的是要注意“标准方法”在某种意义上既不是标准也不是方法。这些被认为是动作,并且只是标准的,因为它们是脚手架使用的约定。您可以创建任意数量的操作并使用控制器对它们进行逻辑分组。

如果您打开[Project]/config/routes.rb并阅读注释,我想您会更好地理解控制器和操作如何映射到特定路线。例如,您可以创建一个到登录控制器的登录操作的命名路由,并通过添加到您的routes.rb顶部来调用它进行身份验证:

# ex: http://localhost/authenticate
map.authenticate 'authenticate', :controller => 'login', :action => 'login'
# map.[route] '[pattern]', :controller => '[controller]', :action => '[action]'

#  ex: http://localhost/category/1
map.category 'category/:id', :controller => 'categories', :action => 'list'

# ex: http://localhost/product_group/electronics
map.search 'product_group/:search', :controller => 'products', :action => 'list'

要部分回答您的问题,您可能需要考虑添加一个类别模型并将所有产品关联到一个类别。然后,您可以添加一个命名路由来按类别查看项目,如上面的代码块所示。

使用命名路由的主要好处是您可以在视图中将它们称为 category_urlcategory_path。大多数人不想这样做,而是依赖于默认路由映射(在routes.rb的末尾):

 # ex: http://localhost/category/view/1
 map.connect ':controller/:action/:id'

 # ex: http://localhost/category/view/1.xml
 # ex: http://localhost/category/view/1.json
 map.connect ':controller/:action/:id.:format'

这里要提到的关键是,当URI匹配路由时,与符号( :id 或 :search) 被传递到 params 哈希中。例如,上面的搜索命名路线会将搜索词匹配到params[:search],因此,如果您的产品有一个名为“type”的字符串列,您计划对其进行搜索,那么您的产品控制器可能会如下所示:

class Products < ApplicationController
  def list
    search_term = params[:search]
    @products = Product.find(:all, :conditions => ["type = ?", search_term])
  end
end

然后,视图 [Project]/app/views/products/list.html.erb 将可以直接访问 @products

如果您确实想要深入了解 Ruby on Rails(可能比您发布的链接中的指南容易理解 10 倍),您应该查看
使用 Rails 进行敏捷 Web 开发:第二版,第二版

First, I think it's important to note that the "standard methods" are neither standard nor methods in a sense. These are considered actions, and are only standard in that they are the conventions used with scaffolding. You can create any number of actions and group them logically with a controller.

If you open up [Project]/config/routes.rb and read through the comments, I think you'll understand a little better how controllers and actions map to a specific route. For instance, you can create a named route to the login controller's login action and call it authenticate by adding to the top of your routes.rb:

# ex: http://localhost/authenticate
map.authenticate 'authenticate', :controller => 'login', :action => 'login'
# map.[route] '[pattern]', :controller => '[controller]', :action => '[action]'

#  ex: http://localhost/category/1
map.category 'category/:id', :controller => 'categories', :action => 'list'

# ex: http://localhost/product_group/electronics
map.search 'product_group/:search', :controller => 'products', :action => 'list'

To partially answer your question, you may want to consider adding a category model and associating all products to a category. then, you can add a named route to view items by category as in the code block above.

The major benefit to using named routes is that you can call them in your views as category_url or category_path. Most people don't want to do this and rely on the default route mappings (at the end of the routes.rb):

 # ex: http://localhost/category/view/1
 map.connect ':controller/:action/:id'

 # ex: http://localhost/category/view/1.xml
 # ex: http://localhost/category/view/1.json
 map.connect ':controller/:action/:id.:format'

The key thing to mention here is that when a URI matches a route, the parameter that matches against a symbol (:id, or :search) is passed into the params hash. For instance, the search named route above would match a search term into params[:search], so if your products have a string column called 'type' that you plan to search against, your products controller might look like:

class Products < ApplicationController
  def list
    search_term = params[:search]
    @products = Product.find(:all, :conditions => ["type = ?", search_term])
  end
end

Then, the view [Project]/app/views/products/list.html.erb will have direct access to @products

If you'd really like an in-depth view into Ruby on Rails (one that is probably 10 times easier to follow than the guide in the link that you posted) you should check out
Agile Web Development with Rails: Second Edition, 2nd Edition

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文