我有一个Rails应用程序,拥有一个拥有很多关系的专辑和歌曲模型。我正在尝试使用simple_form和nested_form宝石将歌曲添加到相册中。如何使用simple_form和嵌套窗体通过关联为has_many创建嵌套窗体?
如果我使用simple_form,创建关联很容易,但是我无法使其与nested_form配合使用。看来这应该工作:
<%= 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>
但我得到这个错误:RuntimeError in Albums#new Association :songs not found
。如果我只是使用simple_form,该协会工作正常。
正确的语法是什么?还是这些宝石不相容?如果这两个宝石不兼容,那么如何使用nested_form添加和删除专辑中的歌曲?
/视图/专辑/ _form https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000
/模型/专辑 https://gist.github.com/leemcalilly/9a16f43106c788ab6877
/模型/歌曲 https://gist.github.com/leemcalilly/0ccd29f234f6722311a0
/模型/ albumization https://gist.github.com/leemcalilly/c627ad2b178e1e11d637
/控制器/ albums_controller https://gist.github.com/leemcalilly/04edf397b2fb2a3d0d1d
/控制器/ songs_controller https://gist.github.com/leemcalilly/bcbccc9259c39d0b6b7a
你可以发布你的相册控制器吗?它看起来就是错误来自的地方。 – 2013-03-17 23:45:55
编辑它以包含我的控制器。 – 2013-03-18 14:38:52
在您的'albuns_controller.rb'的'new'动作中添加一个'@ album.songs.build'。 – MurifoX 2013-03-18 14:42:43