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>
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>-- Posted via http://www.ruby-forum.com/.
Hi Michael, I guess that was quite obvious, but thank you, working like a charm! Sean On 24-Jan-06, at 2:53 AM, Michael Bannister wrote:> 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> > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Seemingly Similar Threads
- How do I show the selected values in options_for_select? Not as simple as it sounds.
- selected attribute in options_for_select
- options_for_select - how to set the selected value?
- Help with Create and Update with options_for_select in a select_tag
- options_for_select - default nil value