Getting multiple select to work: options_for_select

Hi,

<%= select_tag ‘image_id’, options_for_select(Image.find(:all) {|
an_image| [ an_image.name, an_image.id ] }), :multiple => true %>

  • I am in a view, and I am trying to search the image table and make a
    multiple select box with each image’s name.
  • Right now it is working, but I see this:

#Image:0xklasjdka |
#Image:0xklasb987 |
#Image:0xkla9a8sd |
#Image:0xklasasjd v

  • The desired end result should look like this:

top.jpg |
back.jpg |
left.jpg |
upper.jpg v

Can anyone tell me why I am getting this object back like this, and
how to fix it?

-truchty

( ps I am a rails n00b )

Jerzy :

<%= select_tag ‘image_id’, options_for_select(Image.find(:all) {|
an_image| [ an_image.name, an_image.id ] }), :multiple => true %>

You’re attaching a block to Image.find that doesn’t need one.
You should chain with a map method like that :

Image.find(:all).map { |an_image| [ an_image.name, an_image.id ] }

You can use script/console to find the right expression and have
instant feedback.

– Jean-François.