简体   繁体   English

如何通过使用simple_form和嵌套表单的关联为has_many创建嵌套表单?

[英]How to create a nested form for a has_many through association using simple_form and nested form?

I have a rails app that has an album and song model with a has many through relationship. 我有一个Rails应用,其中有专辑和歌曲模型,并且有很多直通关系。 I'm trying to add songs to albums using the simple_form and nested_form gems. 我正在尝试使用simple_formnested_form宝石将歌曲添加到专辑。

If I use simple_form, it's easy to create the association, but I'm having trouble getting it to work with nested_form. 如果使用simple_form,创建关联很容易,但是我很难使它与nested_form一起使用。 It seems that this should work: 看来这应该工作:

<%= f.fields_for :songs do |song_form| %>
  <%= song_form.association :songs %>
  <%= song_form.link_to_remove "Remove this song" %>
<% end %>
<p><%= f.link_to_add "Add a song", :songs %></p>

But I get this error: RuntimeError in Albums#new Association :songs not found . 但是我收到以下错误消息: RuntimeError in Albums#new Association :songs not found The association works fine if I just use simple_form. 如果我只使用simple_form,则关联工作正常。

What would the correct syntax be? 正确的语法是什么? Or are these gems incompatible? 还是这些宝石不兼容? If the two gems are not compatible, then how would you add and remove songs from albums using just nested_form? 如果这两个宝石不兼容,那么如何仅使用nested_form从专辑中添加和删除歌曲?

/views/albums/_form https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000 / views / albums / _form https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000

/models/album https://gist.github.com/leemcalilly/9a16f43106c788ab6877 / models / album https://gist.github.com/leemcalilly/9a16f43106c788ab6877

/models/song https://gist.github.com/leemcalilly/0ccd29f234f6722311a0 / models / song https://gist.github.com/leemcalilly/0ccd29f234f6722311a0

/models/albumization https://gist.github.com/leemcalilly/c627ad2b178e1e11d637 / models /相册https://gist.github.com/leemcalilly/c627ad2b178e1e11d637

/controllers/albums_controller https://gist.github.com/leemcalilly/04edf397b2fb2a3d0d1d / controllers / albums_controller https://gist.github.com/leemcalilly/04edf397b2fb2a3d0d1d

/controllers/songs_controller https://gist.github.com/leemcalilly/bcbccc9259c39d0b6b7a / controllers / songs_controller https://gist.github.com/leemcalilly/bcbccc9259c39d0b6b7a

The form builder song_form represents a Song object, not an Album , this is why the association is not found. 表单生成器song_form表示Song对象,而不是Album ,这就是为什么找不到关联的原因。

In the block following fields_for , you can manually create the form for a song. fields_for的块中,您可以手动创建歌曲的形式。 And like @david mentioned in his comment, you should use simple_fields_for instead of fields_for to get all simple_form methods available . 就像他评论中提到的@david一样,您应该使用simple_fields_for而不是fields_for 来获取所有可用的simple_form方法 Which results in: 结果是:

<%= f.simple_fields_for :songs do |song_form| %>
  <%= song_form.input :artwork %>
  <%= song_form.input :track %>
  ...
  <%= song_form.link_to_remove "Remove this song" %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过关联为has_many创建嵌套资源的表单? - How to create form for nested resource with has_many through association? 与has_many的Simple_Form关联:通过额外字段 - Simple_Form Association with has_many :through extra field Rails 4 has_many通过使用ajax的关联嵌套形式 - Rails 4 has_many through association nested form using ajax 如何使用simple_form通过条件创建与has_many相关的记录 - How to create a record which tied to has_many through with condition using simple_form has_many多对多:通过关联嵌套形式 - many-to-many with has_many :through association nested form 如何创建和提交嵌套表单,其中包含通过关联从 has_many 填充的值? - How to create and submit a nested form with values populated from a has_many through association? has_many与simple_form关联中的唯一属性 - Unique attribute in has_many association with simple_form 如何将嵌套属性与simple_form,Rails 4和has_many一起使用? - How should I use nested attributes with simple_form, Rails 4, and has_many? “无效的关联”:nested_form_for与has_many - “Invalid association”: nested_form_for with has_many through Rails 4通过关联和数据库嵌套表格has_many - Rails 4 Nested form has_many through association and database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM