简体   繁体   English

一个控制器适用于2种型号,是否正确?

[英]One controller for 2 models, is it the right rails way?

I have two models: 我有两个模型:

First one, User, contains the following attribute: email, password and boolean admin. 第一个用户,包含以下属性:电子邮件,密码和布尔admin。 The second one, Information, contains the folllowing attribute: name, surname, date_of_birth, address, city, country, post_code, phone, credit. 第二个信息是包含以下属性:名称,姓氏,出生日期,地址,城市,国家,邮政编码,电话,信用。

Abviously there is a relationship one-to-one, is it right to manage these two models with one controller, for instance, UsersController? 显然存在一对一的关系,用一个控制器(例如UsersController)来管理这两个模型是否正确?

Controllers handle the user interaction. 控制器处理用户交互。 So the the structure of the controllers should be like it makes sense from the user perspective. 因此,从用户的角度来看,控制器的结构应该是合理的。 Models handles the data and the business logic. 模型处理数据和业务逻辑。 But implementation details should not drive the user interface! 但是实现细节不应驱动用户界面! So it makes it makes often sense to have one controller with two models, or a models with several controllers. 因此,使一个控制器具有两个模型或一个具有多个控制器的模型变得很有意义。

It is just that Rails makes it so easy to use one controller with exactly one corresponding model, and it works very often very well. 仅仅是Rails使使用一个控制器和一个正确的相应模型变得如此容易,并且它经常很好地工作。

如果这两个模型受关系约束, 那么accept_nested_attributes_for可能是一个很好的解决方案。

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

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