嵌套模型形式和 update_attributes
我有一个嵌套模型表单,类 Project 和类 TeamMember 之间具有一对多关系,并且在控制器中,有一个更新函数,例如:
@project = Project.find(params[:id])
@project.update_attributes(params[:project])
现在,我想在保存之前为某些团队成员设置一些表单中未设置的字段发生。我无法按原样使用 update_attributes 函数。
最好的方法是什么?
谢谢, 尼古拉斯.
I have a nested-model form with a one-to-many relationship between a class Project and class TeamMember, and in the controller, an update function like :
@project = Project.find(params[:id])
@project.update_attributes(params[:project])
Now, I'd like to set some fields that are not set in the form for some of the team members before the saving happens. I cannot use the update_attributes function as is to do so.
What would be the best way to do it ?
Thanks,
Nicolas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会研究
accepts_nested_attributes_for
函数。您可能会遇到类似的情况:在表单中,您需要使用
fields_for
方法来嵌套属性。这可能看起来像:当您提交表单时,您将能够调用
@project.update_attributes(params[:project])
并且它将起作用。您还可以raise params.inspect
来查看参数是如何嵌套的。希望这有帮助。
I would look into the
accepts_nested_attributes_for
function. You might have something like:In your forms you will want to use the
fields_for
method to nest your attributes. That might look something like:When you submit the form you will be able to call
@project.update_attributes(params[:project])
and it will work. You can alsoraise params.inspect
to see how the params are nested.Hope this helps.