Ruby on Rails - 创建具有关系的实体

发布于 2024-09-05 09:08:05 字数 346 浏览 2 评论 0原文

我是 Rails 新手,所以要友善。

我正在构建一个“rolodex”类型的应用程序,这个问题是关于同时处理创建实体和多个关系实体的最佳方法。

对于(一个人为的)例子: 我的应用程序将有一个 Person 模型,其中 has_one Contact_Info 模型。在 Personcreate.html.erb 页面上,我的应用程序的用户可以同时创建此人和 contact_info。

在人员的创建视图/控制器中直接包含创建联系人的详细信息似乎不正确。处理这个问题的 Rails 方法是什么?

I'm new to rails, so be nice.

I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time.

For (a contrived) example:
My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for Person it makes sense for the user of my appliction to create the person, and the contact_info at the same time.

It doesn't seem right to include details for creating a contact directly in the create view/controller for person. What's the rails way to handle this?

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

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

发布评论

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

评论(3

仙气飘飘 2024-09-12 09:08:05

使用嵌套属性 是最常见的方法。

实际文档位于此处

Using nested attributes is the most common way to do this.

The actual documentation is here.

人疚 2024-09-12 09:08:05

您想使用“嵌套表单”。

You want to use "Nested Forms". There is a great example of them in this blog post.

所有深爱都是秘密 2024-09-12 09:08:05

我也是菜鸟,但我在应用程序上遇到了类似的问题。当时我正在聘请一位导师,他基本上说这是 Rails 固执己见的一个很好的例子。听起来您想同时对两个不同的模型执行创建操作,这可能是可能的,但可能非常困难。我建议考虑是否可以修改您的数据模型,或者找到一种方法在以不同形式收集数据的同时形成可接受的用户流。

更新:在写这篇文章时,技术答案出现了。请记住,如果这样做可以帮助您将应用程序推出市场,那么采取简单的路线是完全可以的,特别是当您还是新手时。

I'm also noob, but I had a similar issue with an app. I was using a tutor at the time and he basically said it was a good example of rails being opinionated. It sounds like you want to take the create action for two different models at the same time, which may be possible but probably very hard. Id suggest considering whether your data model could be modified, or find a way to make an acceptable user flow while collecting the data in different forms.

Update: while writing this the technical answer came in. Keep in mind, its perfectly okay to take the easy route if doing so helps you get the app out the door, and especially while you're still new.

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