Forum: Ruby on Rails problem with options_for_select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alien8 Recordings (Guest)
on 2006-01-24 01:45
(Received via mailing list)
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

<select name="publisher_ids[]">
<%= options_for_select (@publishers.map {|p| [truncate
(p.publisher_name, length = 30, truncate_string = "..."), p.id]},
@book.publishers )%>
</select>
Michael B. (Guest)
on 2006-01-24 09:53
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
>
> <select name="publisher_ids[]">
> <%= options_for_select (@publishers.map {|p| [truncate
> (p.publisher_name, length = 30, truncate_string = "..."), p.id]},
> @book.publishers )%>
> </select>
Alien8 Recordings (Guest)
on 2006-01-24 18:19
(Received via mailing list)
Hi Michael,

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

Sean
This topic is locked and can not be replied to.