Hi, I''m quite new to this language and I''m having trouble finding out simple things like a dropdown menu in rhtml. I know I have to use <% select_tag %> but I''ve read lots of different ways to put in the options so I''m a bit confused. Options are hard-coded for the menu I''m trying to create so no database is involved! Thanks v much! Bex -- Posted via http://www.ruby-forum.com/.
Hi Becky -- On 13-Jul-06, at 4:14 AM, Becky Franklin wrote:> Hi, I''m quite new to this language and I''m having trouble finding out > simple things like a dropdown menu in rhtml. I know I have to use <% > select_tag %> but I''ve read lots of different ways to put in the > options > so I''m a bit confused. Options are hard-coded for the menu I''m > trying to > create so no database is involved!You can do it one of two ways: supply a string of option tags as the second argument to select_tag, or use the options_for_select helper to generate the option tags for you. Here''s an example of the former. select_tag ''categories'', ''<option>Ruby</option><option>Rails</option>'' That''s pretty ghetto, though, what with all those option tags; you should use the options_for_select helper instead, passing in your options as an array: select_tag ''categories'', options_for_select([''Ruby'', ''Rails'']) Or, assuming you want ''values'' for each of your options, pass in Hash who''s keys are the names of the options and who''s values are the value attributes: select_tag ''categories'', options_for_select({''Ruby'' => ''1'', ''Rails'' => ''2''}) If you want to pre-select a certain option, the second argument to options_for_select is the cheese: select_tag ''categories'', options_for_select({''Ruby'' => ''1'', ''Rails'' => ''2''}, ''1'') The final example would output something like: <select id="categories" name="categories"> <option value="1" selected="selected">Ruby</option> <option value="2">Rails</option> </select> HTH /Jeff -- http://re.visioni.st http://quotedprintable.com
I''m not sure about v1.2, but from the below snippet, ''Ruby'' is pre-selected. If i choose ''Rails'', and refresh my browser, ''Rails'' would still be chosen instead of ''Ruby''. Ideally, I''d like ''Ruby'' to be selected again if the page refreshes.. ideas anyone? D Jeffrey Hardy wrote:> Hi Becky -- > > > If you want to pre-select a certain option, the second argument to > options_for_select is the cheese: > > select_tag ''categories'', options_for_select({''Ruby'' => ''1'', ''Rails'' > => ''2''}, ''1'') > > The final example would output something like: > > <select id="categories" name="categories"> > <option value="1" selected="selected">Ruby</option> > <option value="2">Rails</option> > </select> > > HTH > > /Jeff > > -- > http://re.visioni.st > http://quotedprintable.com-- 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 -~----------~----~----~----~------~----~------~--~---