Sandeep Gudibanda
2008-May-20 08:42 UTC
Struggling with Date fields - select_month and select_year
Hi, I collect month and year from user for a particular field called start date. Year is mandatory where as month is optional. Initially i used date_select for the field, but moved away when i learnt that in date_select if user doesnt choose the month, month is set to Jan by default. So i thought of using, separate fields for month and year of type integer. Tried using select_date and select_year, but could not set default to blank in those methods. So moved to plain html listing for select: Shown for date below select id="magazine_start_date_month" name="magazine[start_date_month]"> <option value=""></option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> and similarly for year too. But when i tied to edit this, the fields are not getting populated but value is stored correctly in db. Any suggestions? I knw i have done many foolish/naive things to get here, but i have found no other way. Regards, Sandeep G -- 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 -~----------~----~----~----~------~----~------~--~---
Greg Willits
2008-May-20 09:42 UTC
Re: Struggling with Date fields - select_month and select_year
On May 20, 2008, at 1:42 AM, Sandeep Gudibanda wrote:> So moved to plain html listing for select: Shown for date below > > select id="magazine_start_date_month" name="magazine > [start_date_month]"> > <option value=""></option> > <option value="1">January</option> > <option value="2">February</option> > <option value="3">March</option> > <option value="4">April</option> > <option value="5">May</option> > <option value="6">June</option> > <option value="7">July</option> > <option value="8">August</option> > <option value="9">September</option> > <option value="10">October</option> > <option value="11">November</option> > <option value="12">December</option> > </select> > and similarly for year too. > > But when i tied to edit this, the fields are not getting populated but > value is stored correctly in db.If that''s your exact code, then you haven''t provided for any way to mark which item has been selected. Just like you have to populate the value="" attribute in <input> tags, you have to populate the selected="selected" attribute of the <select> tag. Here''s the tedous way to do that, which whos you the prinicple. It assumes your form data is in <select id="magazine_start_date_month" name="magazine [start_date_month]"> <option value=""></option> <option value="1"<%= if magazine.start_date_month == ''1''; '' selected="selected"''; end %>>January</option> <option value="2"<%= if magazine.start_date_month == ''2''; '' selected="selected"''; end %>>February</option> <option value="3"<%= if magazine.start_date_month == ''3''; '' selected="selected"''; end %>>March</option> ... </select> Of course doing that manually for all select tags in your app can be tedious and prone to errors, so writing a generic routine to draw these things is usually a good idea. I have a general purpose library for defining, caching, and drawing value lists like this. -- greg willits --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sandeep Gudibanda
2008-May-20 10:14 UTC
Re: Struggling with Date fields - select_month and select_ye
hmmm. pretty tedious. Why doesn''t select_month and select_year doesn''t provide options to choose default and also suport for form variables, just the way date_select is done. Or do they provide and I am just blind to it? -- 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 -~----------~----~----~----~------~----~------~--~---
Sandeep Gudibanda
2008-May-20 10:24 UTC
Re: Struggling with Date fields - select_month and select_ye
Thanks Greg. I managed to write a helper function to return the option tag with selected = "selected" set. Helper Function: def build_selected_option_for_months(value) months = Date::MONTHNAMES return ''<option value="'' + value.to_s + ''"selected="selected">'' + months[value] + ''</option>'' end In the view: option value=""></option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> <%= build_selected_option_for_months(@user_company.start_date_month) %> </select> Thanks a lot again. Regards, Sandeep G Sandeep Gudibanda wrote:> > hmmm. pretty tedious. > > Why doesn''t select_month and select_year doesn''t provide options to > choose default and also suport for form variables, just the way > date_select is done. > Or do they provide and I am just blind to it?-- 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 -~----------~----~----~----~------~----~------~--~---