4

I have a rails app that has an album and song model with a has many through relationship. I'm trying to add songs to albums using the simple_form and nested_form gems.

If I use simple_form, it's easy to create the association, but I'm having trouble getting it to work with 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. The association works fine if I just use 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?

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

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

/models/song 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

Lee McAlilly
  • 9,084
  • 12
  • 60
  • 94
  • 1
    Can you post your Albums controller? It looks like that's where the error is coming from. – Peter Brown Mar 17 '13 at 23:45
  • Edited it to include my controllers. – Lee McAlilly Mar 18 '13 at 14:38
  • Add a `@album.songs.build` to your `new` action on your `albuns_controller.rb`. – MurifoX Mar 18 '13 at 14:42
  • Ok, not sure what you're going for. If I update my albums controller to this - https://gist.github.com/leemcalilly/da673a9240b0517de274 Then, I get an `association :songs not found` error that looks like this - https://www.evernote.com/shard/s3/sh/d45bfaea-56ba-4076-ac29-0edb027b694c/fae3205468a5b62dce26f2c0916eacfa – Lee McAlilly Mar 18 '13 at 15:04
  • Ok I see what you mean. I need to build an initial song even if it's nil so you just simply add @album.songs.build to the new action. Now I have this: https://gist.github.com/leemcalilly/9f2e3c1670837f8a3242 but I'm still getting `Association :songs not found` – Lee McAlilly Mar 20 '13 at 02:26
  • I also tried adding `attr_accessible :albumizations_attributes` to my album model and that didn't help. – Lee McAlilly Mar 20 '13 at 02:29
  • Ditto for adding `attr_accessible :songs_attributes` to my album model. – Lee McAlilly Mar 20 '13 at 02:32
  • Have you tried calling `<% f.simple_fields_for %>` I believe `fields_for` should be the plain form builder iirc – Deej Mar 20 '13 at 14:53
  • Yeah I did try that still get the `Association :songs not found` error. I updated my _form gist in the original question - https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000 – Lee McAlilly Mar 20 '13 at 15:27
  • see siekfried's answer. get rid of `<%= song_form.association :songs %>`, as explained `song_form` refers to `song` object not `album` – tw airball Mar 24 '13 at 02:21
  • I added a comment to siekfried's answer below. I don't want to create songs within the album form, just create new associations between albums and songs. Basically just add pre-existing songs to the album and I'm having trouble figuring out the correct syntax for this. – Lee McAlilly Mar 26 '13 at 19:58

1 Answers1

6

The form builder song_form represents a Song object, not an Album, this is why the association is not found.

In the block following fields_for, you can manually create the form for a song. And like @david mentioned in his comment, you should use simple_fields_for instead of fields_for to get all simple_form methods available. 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 %>
siekfried
  • 2,964
  • 1
  • 21
  • 36
  • I'm trying to create the association between the album and song rather than create a song through the album form. I'm creating the songs with a different song form. Just putting the fields for the song as inputs doesn't create the association with a pre-existing song. I tried this https://gist.github.com/leemcalilly/699cbac054849b407353, but that gave me an error as well. Is there some syntax for creating an association that I'm missing? Is this possible with simple_nested_form_for? If not, what would be the syntax for a normal rails has_many :through association in this form? – Lee McAlilly Mar 26 '13 at 19:57