4

私は、多くの関係を持つアルバムと曲のモデルを持つレールアプリを持っています。simple_formおよびnested_formジェムを使用してアルバムに曲を追加しようとしています。

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 を使用するだけで、関連付けは正常に機能します。

正しい構文は何でしょうか? それとも、これらの宝石は互換性がありませんか? 2 つの gem に互換性がない場合、nested_form だけを使用してどのようにアルバムから曲を追加および削除しますか?

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

/モデル/アルバム https://gist.github.com/leemcalilly/9a16f43106c788ab6877

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

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

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

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

4

1 に答える 1

6

フォーム ビルダーはではなくオブジェクトをsong_form表します。これが関連付けが見つからない理由です。SongAlbum

次のブロックfields_forでは、曲のフォームを手動で作成できます。@david のコメントで言及されているように、すべての simple_form メソッドを利用可能にsimple_fields_forする代わりに使用する必要があります。結果は次のとおりです。fields_for

<%= 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 %>
于 2013-03-20T16:28:07.567 に答える