轨道 3 +设计:新评论表单还包含用户模型字段?
这个想法是拥有您以前见过一百万次的“发布新评论”表单,其中包含以下字段(适用于未登录的访问者):姓名、电子邮件、网站和消息,
请注意“消息”字段属于Comment模型,另外3个属于User模型。由于用户模型是设计可验证的,我希望能够在这种混合形式中使用这些验证(以及评论字段的验证)。
这可能吗?即使 Comment 是 User 的子级,我是否可以将 accepts_nested_attributes_for(:user)
添加到 Comment 模型?
谢谢!
The idea is to have what you've seen a million times before, a "post a new comment" form with the following fields (for visitors who are not logged in): Name, Email, Website, and Message
Note the "message" field belongs to the Comment model, and the other 3 belong to the User model. Since the User model is Devise validatable, I'd like to be able to use those validations (as well as those for the comment field) in this hybrid form.
Is this possible? Can I add accepts_nested_attributes_for(:user)
to the Comment model, even though the Comment is the child of User?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否正是您所要求的,但您可以执行类似的操作...在您的用户模型中创建一个虚拟访问器以进行评论:
然后在 app/views/users/registrations/new 中。 html.erb:
我还没有测试过这个,但它是根据我正在使用的类似工作代码改编的。
这实际上是添加了评论的用户表单。我只是让设计做它该做的事,尽量不去对抗它。
I'm not sure if this is exactly what you're asking for, but you can do something like this ... make a virtual accessor in your User model for a comment:
Then in app/views/users/registrations/new.html.erb:
I haven't tested this but it's adapted from similar working code that I'm using.
This is actually the user form with a comment added. I just let devise do its thing and try not to fight it.