Rails 3新模型接受_nested_attributes_for形式(而不是完整集合)

发布于 2024-10-20 11:27:44 字数 911 浏览 2 评论 0原文

我有一个模型专辑,其中有一个名为 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 技术交流群。

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

发布评论

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

评论(2

怪我鬧 2024-10-27 11:27:44

相反,

Track.new

您需要

@track = @album.tracks.build

在控制器中进行设置并使用它。这应该通过验证错误保留字段。

Instead of

Track.new

you need to set up

@track = @album.tracks.build

in your controller and use that. That should preserve the fields through the validation errors.

旧人 2024-10-27 11:27:44

我找到了解决方案。它有效,但我想知道这是否是一个好的解决方案。感谢 Srdjan 让我走上正轨!

@track = @album.tracks.build

form.semantic_fields_for :tracks, @track do | builder | 
   builder.fields if builder.object.new_record?
end

显然只呈现新记录的表单字段。

I found a solution. It works, but I wonder if it is a good solution. Thanks Srdjan for putting me on the right track!

@track = @album.tracks.build

form.semantic_fields_for :tracks, @track do | builder | 
   builder.fields if builder.object.new_record?
end

Apparently only renders the new record's form fields.

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