Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a Select with Model Data in Rails

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.

like image 973
Rob Wilkerson Avatar asked Sep 25 '09 13:09

Rob Wilkerson


2 Answers

I think that select elements are one of the more confusing aspects of Rails, because as you said there seem to be a number of ways to do it.

Try this:

<%= f.select(:album_id, @image.albums.all.collect {|a| [a.name, a.id]}) -%>
like image 198
John Topley Avatar answered Oct 17 '22 19:10

John Topley


Well, I'm not sure it's the best way, the Rails way or, frankly, even an elegant way, but here's the code I've used that seems to be working so far.

<%= f.label 'Albums' -%>
<%= collection_select( :image, :album_ids, current_user.albums, :id, :name, {}, { :multiple => true } ) -%>

At this point, when I say "working", all I can really attest to is that the page renders with no errors and the appropriate album or albums are selected when I edit an image. I'm still shocked at how difficult it was to cobble together a "full" solution from a lot of disparate sources.

like image 27
Rob Wilkerson Avatar answered Oct 17 '22 17:10

Rob Wilkerson