开发者

Rails convention

开发者 https://www.devze.com 2023-01-24 05:51 出处:网络
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.

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.

One of my concern is the following situation using Rails convention:

  1. I create a table called users
  2. I create a model called User.cfc
  3. I create a controller called 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.

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. 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"?开发者_StackOverflow


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.

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.

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. You may have some Models without their own Controllers (e.g. Login, EmailAddress), and some Controllers with no associated models (e.g. Home, Admin).

0

精彩评论

暂无评论...
验证码 换一张
取 消