Rails 3新模型接受_nested_attributes_for形式(而不是完整集合)
我有一个模型专辑,其中有一个名为 Track 的“has_many :trough”关联。基本上,一张专辑可以包含多首曲目,但一首曲目可以同时属于多个专辑(因此称为“曲目列表”)。
我有一个 RESTful 应用程序,其中包含一个表单(在 formtastic 的帮助下),该表单列出了所有曲目的属性,如下所示
form.semantic_fields_for :tracks do | builder |
builder.fields
end
该表单列出了所有关联的曲目字段,我可以很好地更新它们,一切正常。
现在,我想做的是有另一个视图来将新曲目添加到现有专辑中。在此视图中,我不想渲染现有轨道的所有输入字段,而只想渲染我尝试创建的单个新关联的字段。
我试图找到有关如何执行此操作的信息,我想出的最好的方法是:
form.semantic_fields_for :tracks, Track.new do | builder |
builder.fields
end
这将输出一组所需的轨道输入,我可以提交表单,轨道将被创建并关联到产品 - 但前提是没有验证错误。
问题来自于验证。如果失败,应用程序会再次呈现相同的视图,但所有输入再次为空(所有数据,无论是否有效,都会丢失,并且 Formtastic 的内联错误消息也消失):
form.semantic_fields_for :tracks, Track.new do | builder |
builder.fields
end
我认为这是因为我做了Track.new 即使请求参数中已经存在一个包含数据和错误消息的对象。这当然不好。我想要的是显示已输入数据的单轨表单,以及 Formtastic 错误消息。
我做错了什么?
I have a model Album which has a "has_many :trough" assocation named Track. Basically, an album can have a number of tracks on it, but a track can belong to more than one album at the same time (hence the through association "Tracklist").
I have a RESTful app with a form (with the help of formtastic) that puts out all the tracks's attributes like so
form.semantic_fields_for :tracks do | builder |
builder.fields
end
The form puts out all the associated Tracks' fields and I can update them fine, everything works.
Now, what I want to do is have another view to add a new track to an existing album. In this view, I don't want to render all the input fields for the existing tracks, but only the fields for the single new association I am trying to create.
I have tried to find info on how to do this, and the best I've come up with is:
form.semantic_fields_for :tracks, Track.new do | builder |
builder.fields
end
This outputs a single set of Track inputs, like needed, and I can submit the form and the track gets created and associated to the product - but only if there are no validation errors.
The problem comes with validations. If one fails, the app renders the same view again, but all the inputs are empty again (all data, valid or not, is lost, and the inline error messages for Formtastic are also gone):
form.semantic_fields_for :tracks, Track.new do | builder |
builder.fields
end
I reckon this is because I do a Track.new even though there is already an object in the request params which has the data and error messages. This is no good of course. What I want to have is the form for the single track displayed with the already input data, as well as the Formtastic error messages.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,
您需要
在控制器中进行设置并使用它。这应该通过验证错误保留字段。
Instead of
you need to set up
in your controller and use that. That should preserve the fields through the validation errors.
我找到了解决方案。它有效,但我想知道这是否是一个好的解决方案。感谢 Srdjan 让我走上正轨!
显然只呈现新记录的表单字段。
I found a solution. It works, but I wonder if it is a good solution. Thanks Srdjan for putting me on the right track!
Apparently only renders the new record's form fields.