Problem with options_for_select


#1

Hi All,

I am having a little difficulty using the options_for_select forms
helper method. I have a form for an active record object book, which
belongs to a publisher. Using the options_for_select is working fine
as far as creating the various options, but it will not output the
“selected=‘selected’” bit of html needed to identify which publisher
is associated with a give book in the database. In order to so so I
am passing it the @book.publishers array, but I suppose I am missing
something. The docs say that the selected element may be an array of
values, but I can’t get it to work. Any ideas? My code is below.
Thanks in advance.

Sean

<%= options_for_select (@publishers.map {|p| [truncate (p.publisher_name, length = 30, truncate_string = "..."), p.id]}, @book.publishers )%>

#2

You need to give it an id (or perhaps array of ids), so as your second
parameter try @book.publishers.map {|p| p.id}
(I think…)

Michael

Alien8 Recordings wrote:

Hi All,

I am having a little difficulty using the options_for_select forms
helper method. I have a form for an active record object book, which
belongs to a publisher. Using the options_for_select is working fine
as far as creating the various options, but it will not output the
“selected=‘selected’” bit of html needed to identify which publisher
is associated with a give book in the database. In order to so so I
am passing it the @book.publishers array, but I suppose I am missing
something. The docs say that the selected element may be an array of
values, but I can’t get it to work. Any ideas? My code is below.
Thanks in advance.

Sean

<%= options_for_select (@publishers.map {|p| [truncate (p.publisher_name, length = 30, truncate_string = "..."), p.id]}, @book.publishers )%>

#3

Hi Michael,

I guess that was quite obvious, but thank you, working like a charm!

Sean