简体   繁体   English

Rails从父控制器更新子级

[英]Rails update child from parent controller

So I have this simple situation: 所以我有一个简单的情况:

User model has_one client I want to update client's attributes from user's update method (in controller) Should I accept nested attributes from client and update all as one, or should I update client independently (maybe call client's edit url from here) ? 用户模型has_one客户端我想通过用户的update方法(在控制器中)更新客户端的属性,我应该接受客户端的嵌套属性并将其全部更新为一个,还是应该独立更新客户端(也许从此处调用客户端的编辑url)?

Not sure what is the best way to do this. 不确定执行此操作的最佳方法是什么。

Thanks in advance 提前致谢

If you just want to update the child entity, you should do that into the ChildsController . 如果你只是想更新子实体 ,你应该做的到ChildsController

You can update the child in the parent controller, but it is a violation of the REST principles. 可以在父控制器中更新子代,但这违反了REST原则。 You are the ultimate authority in your code for what you can and cannot do, but following best practices will help you in the long run. 对于您可以做什么和不能做什么,您是代码的最终权威,但是从长远来看,遵循最佳实践将为您提供帮助。

If you insist on following the dark side, I suggest you to just update the child without using the overcomplicated nested_attributes which use case is for a simultaneous update. 如果您坚持要遵循阴暗面,我建议您仅更新子项,而不要使用过于复杂的nested_attributes ,该用例用于同时更新。

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

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