views:

259

answers:

1

I would like to use two picklists to control entry into a date field; I'm concerned with the month and year, not the day. So far, I have the following:

<tr>
  <td>Date Range:</td>
  <td>
    <%= select_month(Date.today, :field_name=>"dateStarted") %>
    <%= select_year(Date.today, :field_name=>"dateStarted") %>
    &nbsp;to&nbsp;
    <%= select_month(Date.today, :field_name=>"dateEnded") %>
    <%= select_year(Date.today, :field_name=>"dateEnded") %>
  </td>
</tr>

This will correctly set the month and year corresponding to today's date. Unfortunately, the select fields do not respond to values in the respective fields. What am I missing?

Moreover, I assume that I will need to combine these two fields in the controller's create and update actions to create an actual date that will be stored in the db, but I am unclear how to do so.

Help is appreciated.

+1  A: 

Why not just use date_select (rather than select_month and select_year)

date_select("dateRange","dateStarted", {:default => Date.today, :discard_day => true})
date_select("dateRange","dateEnded", {:default => Date.today, :discard_day => true})

This will create parameters dateRange[dateStarted] and dateRange[dateEnded] with sub-selects for year and month.

One other clarification, to make it work seamlessly with the create and update action, you should replace "dateRange" with the actual model name. That way it will be properly embedded in the parameter hash

naven87
This approached worked well, thanks.This expression worked: <%= f.date_select(:dateStarted, {:order=> [:month,:year], :prompt=>true, :default => Date.today, :discard_day => true}) %> to <%= f.date_select(:dateEnded, {:order=> [:month,:year], :prompt=>true, :default => Date.today, :discard_day => true}) %>I there a simple way to force date_select to use the end of the month, rather than the first when discard_day=>true?
Craig