I feel the need to apologize for asking such a simplistic question, but I'm getting increasingly frustrated with the Rails Guides. I'm sure they answer my question, but they don't provide enough context for me to really understand how to apply what they're giving me. Nor is Google much help, though I may just be searching the wrong terms/phrases. Given that disclaimer, I'm just going to go ahead and ask:
I have an Image
that HABTM Album
. To support that, I have an albums_images
table with image_id
and album_id
fields (no others). For the life of me, I can't figure out how to populate my image form partial so that the user can select the albums a newly uploaded image should belong to.
I'm learning Rails, so I really just want the basics. I'm sure there are fancy plugins to do this a hundred ways, but I'd like to learn the basics first and build from there. My form partial is pretty much textbook:
<% form_for( @image, :html => { :multipart => true } ) do |f| %>
# All the basics you'd expect to see.
<% end %>
My most recent attempt doesn't work any better than any other variation I've tried, but it looks like this:
<p>
<%= f.label :album_id %>
<%= f.select( :album_id, current_user.albums, :id, :name ) -%>
</p>
Again, I recognize the simplicity of the question I'm asking and I've read what I can find, but I haven't been able to put it together into a complete solution. There seem to be many ways to do it, but no real discussion of each one, their pros/cons or how to really use them in a larger context.
Thanks.
UPDATE: A couple of keys to note and a code correction. First, there's a HABTM relationship between images and albums. Neither model table has a FK directly referencing the other. Second, the album collection should be accessed as current_user.albums
(corrected above). A user has_many
albums and an album belongs_to
user.
UPDATE: At the request of theIV below, at the moment, with this code:
22: <p>
23: <%= f.label :album_id %>
24: <%= f.select( :album_id, current_user.albums.collect {|a| [a.name, a.id]}) -%>
25: </p>
I get this error:
undefined method `album_id' for #<Image:0x1042ec110>
I get the error in line 24.