简体   繁体   English

Rails约定

[英]Rails convention

I'm really new to Rails (for coldfusion) and before I even start coding I want to make sure I understand the convention and apply it right. 我真的是Rails的新手(冷融合),在开始编码之前,我想确保自己了解该约定并正确应用它。

One of my concern is the following situation using Rails convention: 我关注的问题之一是使用Rails约定的以下情况:

  1. I create a table called users 我创建一个称为用户的表
  2. I create a model called User.cfc 我创建一个名为User.cfc的模型
  3. I create a controller called Users.cfc 我创建一个名为Users.cfc的控制器
  4. create a register page so I will add a method called register in the controller Users.cfc since its specifically related to the model User. 创建一个注册页面,因此我将在控制器Users.cfc中添加一个称为register的方法,因为该方法与模型User特别相关。

But now lets say I create a method that call multiple model then where should I put that method? 但是现在让我们说我创建了一个调用多个模型的方法,那么我应该在哪里放置该方法呢?

Example: I'll take facebook wall post for this example. 示例:我将以Facebook墙贴为例。 For each of my post many comments can be added. 对于我的每个帖子,可以添加许多评论。 So I could create a method name postMessage so in that method I would call the model "Post" and also the model "Postcomment" so my question is should I put the method postMessage in the Controller "Posts" or "Postcomments"? 因此,我可以创建一个名为postMessage的方法,因此在该方法中,我将模型称为“ Post”,还将模型称为“ Postcomment”,所以我的问题是我应该将方法postMessage放在控制器的“ Posts”还是“ Postcomments”中?

That really depends on the purpose of the method. 这确实取决于方法的目的。 If it's a user who's looking at his collection of Widgets, you might create a "widgets" method in the Users controller. 如果是用户正在查看其小部件集合,则可以在Users控制器中创建一个“ widgets”方法。

On the other hand, if you want to list all the users who bid on Widget #32, then you might add a "users" method to the Widgets controller. 另一方面,如果要列出所有对Widget#32出价的用户,则可以向Widgets控制器添加“ users”方法。

There is no definite rule with these types of things. 这些类型的事物没有明确的规则。 While you generally want a 1-to-1 correlation between Models and Controllers in Rails, there are exceptions. 通常,您希望在Rails中的模型和控制器之间建立1对1的关联,但也有例外。 You may have some Models without their own Controllers (eg Login, EmailAddress), and some Controllers with no associated models (eg Home, Admin). 您可能有一些没有自己的控制器的模型(例如,登录名,EmailAddress),有一些没有关联模型的控制器(例如,家,管理员)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM