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]”>

January February March April May June July August September October November December 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

On May 20, 2008, at 1:42 AM, Sandeep G. wrote:

June 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
tags, you have to populate the selected=“selected” attribute of the
tag.

Here’s the tedous way to do that, which whos you the prinicple. It
assumes your form data is in

>January >February >March ...

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

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?

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] + ‘’
end
In the view:

option value="">

January February March April May June July August September October November December <%= build_selected_option_for_months(@user_company.start_date_month) %> Thanks a lot again.

Regards,
Sandeep G

Sandeep G. 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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs