The ":selected" statement is not working. Ideas why? Similar experiences? <%= start_form_tag :action => ''dummy'' %> <% @teams = Team.find(:all) %> <label for="team_id">Team</label><br/> <%= collection_select("team", "id", @teams, :id, :name, { :include_blank => true, :selected => @teams[5].id }) %> <%= end_form_tag %> It is generating the follwing code: <form action="/games/dummy" method="post"> <label for="team_id">Team</label><br/> <select id="team_id" name="team[id]"><option value=""></option> <option value="1">Baltimore</option> <option value="2">Buffalo</option> <option value="3">Cincinnati</option> <option value="4">Cleveland</option> <option value="5">Denver</option> <option value="6">Houston</option> <option value="7">Indianapolis</option> <option value="8">Jacksonville</option> <option value="9">Kansas City</option> ... etc </select> </form> Oddly, the option :for include_blank is working, while the selected right next to it is not. Please help. -- (********************************************************** * Peter H. Boling * Web Application Designer - PanEther, LLC * email: peter.boling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org * blog: http://galtzo.blogspot.com/ * languages: English, Spanish, Portuguese ***********************************************************) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try this: <%= collection_select("team", "id", @teams, :id, :name, {:include_blank => true}, {:selected => @teams[5].id }) %> I think :selected belongs in html_options instead of options: collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) Aaron On Aug 31, 2006, at 7:33 PM, Peter Boling wrote:> > The ":selected" statement is not working. Ideas why? Similar > experiences? > > <%= start_form_tag :action => ''dummy'' %> > <% @teams = Team.find(:all) %> > <label for="team_id">Team</label><br/> > <%= collection_select("team", "id", @teams, :id, :name, { > :include_blank => true, :selected => @teams[5].id }) %> > <%= end_form_tag %> > > It is generating the follwing code: > > <form action="/games/dummy" method="post"> > <label for="team_id">Team</label><br/> > <select id="team_id" name="team[id]"><option value=""></option> > <option value="1">Baltimore</option> > <option value="2">Buffalo</option> > <option value="3">Cincinnati</option> > <option value="4">Cleveland</option> > <option value="5">Denver</option> > <option value="6">Houston</option> > <option value="7">Indianapolis</option> > <option value="8">Jacksonville</option> > <option value="9">Kansas City</option> > > ... etc > > </select> > > </form> > > Oddly, the option :for include_blank is working, while the selected > right next to it is not. > > Please help. > > -- > (********************************************************** > * Peter H. Boling > * Web Application Designer - PanEther, LLC > * email: peter.boling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > * blog: http://galtzo.blogspot.com/ > * languages: English, Spanish, Portuguese > ***********************************************************) > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Aaron''s solution won''t work, unfortunately. And although there is a way to pre-select an item by setting @team.id = @teams[5].id for a select list like select(:team, :id, Team.find_all.collect {|p| [ p.name, p.id ] }, { :include_blank => true }) that possibility is missing for collection_select. But for your described needs the select list should work perfectly, so you don''t have to bother about shortcomings of collection_select lists :-P The best way to pre-select an item in a collection_select list is -- as far as I know -- using a separate options_from_collection_for_select for which you can define a selected_value as last parameter. The downside, however, is that such a options list is working with select_tag only. So you have to handle the return value a bit differently in your controller (which should be no problem in most cases). Another drawback is the missing possibility of defining :include_blank for neither select_tag nor options_from_collection_for_select (yet). But you can add an empty item to top easily before passing the values hash to the form helper. A quick outline of this solution: select_tag(:team_id, options_from_collection_for_select(@teams, :id, :name, @teams[5].id)) ...untested. -- 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 -~----------~----~----~----~------~----~------~--~---
Amendment: The collection_select bug has been fixed, eventually. "...This is outdated. You can just use collection_select. If it is already set, it will select the correct option for you." http://wiki.rubyonrails.org/rails/pages/HowtoUseFormOptionHelpers -- 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 -~----------~----~----~----~------~----~------~--~---