How do I specify which option should be initially selected when using the collection_select command? I''ve tried: collection_select(:selectionfilter, :producer_id, Producer.find(:all), :id, :name, {:include_blank => true, :selected_value => @initial_selection}) However it doesn''t return the desired results (the option with the value of @initial_selection selected). I''ve also tried: collection_select(:selectionfilter, :producer_id, Producer.find(:all), :id, :name, {:include_blank => true}, {:selected_value => @initial_selection}) which likewise doesn''t work. I''ve verified that @initial_selection is returning the correct value. The docs say that collection_select uses the options_for_collection_select, which has :selected_value as one of its options, so it seems the above should work, but I''m obviously missing something. -- Posted via http://www.ruby-forum.com/.
On Sat, 2006-06-10 at 10:10 +0200, Fran?ois Montel wrote:> How do I specify which option should be initially selected when using > the collection_select command? I''ve tried: > > collection_select(:selectionfilter, :producer_id, Producer.find(:all), > :id, :name, {:include_blank => true, :selected_value => > @initial_selection}) > > However it doesn''t return the desired results (the option with the value > of @initial_selection selected). > > I''ve also tried: > > collection_select(:selectionfilter, :producer_id, Producer.find(:all), > :id, :name, {:include_blank => true}, {:selected_value => > @initial_selection}) > > which likewise doesn''t work. I''ve verified that @initial_selection is > returning the correct value. > > The docs say that collection_select uses the > options_for_collection_select, which has :selected_value as one of its > options, so it seems the above should work, but I''m obviously missing > something.---- that''s not how I would use it. Assuming that ''your model'' is related to producers, I would use it something more like this... (controller code) @producer = Producer.find(:all) (view code) <%= options = [[''Select'', '''']] + @producer.collect { |producer| [producer.name, producer.id] } select ''YOUR_MODEL'', ''producer_id'', options %> Craig
Craig White wrote:> (controller code) > @producer = Producer.find(:all) > > (view code) > <%= options = [[''Select'', '''']] + @producer.collect { > |producer| [producer.name, producer.id] } > select ''YOUR_MODEL'', ''producer_id'', options %>Thanks, Craig, but your code above yields the same results that I was getting. (It seems to me, my original code is a shortcut for the above.) The problem I''m having is not with the select box malfunctioning, but rather that the select box is not displaying the initial value that I want it to. It''s used to set a filter, and when the user selects a value, the page is refreshed, filtered on that value. At that point I would like the select box to display the last value selected (the value that is being filtered on), which I have stored in @initial_selection. However, despite using the ":selected" option, the select box still show the default first value. That''s what I''m trying to solve. In other words, my code is yielding this: <select id="selectionfilter_producer_id" name="selectionfilter[producer_id]"> <option value=""></option> <option value="1">Bob</option> <option value="2">Harry</option> <option value="3">John</option> when I want it to yield this (assuming the user chose "Harry" last time around). <select id="selectionfilter_producer_id" name="selectionfilter[producer_id]"> <option value=""></option> <option value="1">Bob</option> <option selected value="2">Harry</option> <option value="3">John</option> Any other clues? Thanks! -- Posted via http://www.ruby-forum.com/.
Francois, I was having the same problem and found this: http://wiki.rubyonrails.org/rails/pages/HowtoUseFormOptionHelpers It worked for me, but remember that you may need to cast your initial_selection to an integer (ie @initial_selection.to_i) Hope it works, Andy -- ______________________________ Andrew Biller Broadcast television engineering Consultancy System design Technical support T: +44(0)7768 014 013 F: +44(0)8707 515 078 E: a.biller@ashho.co.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060615/d15e9e84/attachment.html
Francois, did you figure out a way to do this? Do you know how to set multiple options to be selected? Thanks, Wes -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---