The date select helper gives you an easy way to include a date selector within your application. The user selects the date from three dropdown boxes and therefore has little opportunity to make mistakes.
The date selector is created by this statement
<%= date_select( "member", "birthday", :start_year => 1900 ) %> </pre>In this example Member is a model, birthday is a attribute in the model. The selector will start in the year 1900.
The example would render to this html-code in the view :<select name="member[birthday(1i)]"> <option>1900</option> <option>1901</option> [SNIP] <option>1974</option> <option>1975</option> <option selected="selected">1976</option> <option>1977</option> <option>1978</option> <option>1979</option> <option>1980</option> <option>1981</option> </select> <select name="member[birthday(2i)]"> <option value="1">January</option> <option value="2">February</option> <option value="3" selected="selected">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> <select name="member[birthday(3i)]"> <option>1</option> <option>2</option> <option>3</option> [SNIP] <option>21</option> <option selected="selected">22</option> <option>23</option> [SNIP] <option>31</option> </select>As you can see there are the three dropdown boxes, each with a special name, so that rails can use them for a multi-value assignment.
Rails makes it simple to save the changed values.
This is an example code, taken from the member controller :
def update @member = Member.find( @params['member']['id']) if @member.update_attributes( @params['member'] ) redirect_to( :action => "list") else render_action "edit" end endMore on datehelpers is found in the reference documentation
Note on usability
I’m rather wary of this feature. If you use the database a lot, you’d want a simple text-field for date-entry and not click for every date on three select-boxes each. — vs
Defaults
It seems like it’s not possible to set a default for month, day selects. On the other hand, a default can be set for the year using :default_start_year or :start_year. — Tamer Salama?
Answer: Initialize your model with the date you want. More here .
:discard_year doesn’t work!
I have created a small plugin to adjust the behaviour to fix this issue here: http://www.markpaxton.net/RoR/datetime-fix.zip
The selectors also ignore standard rules for the number of days in the month. Each month gives 31 days as valid select options. Ugh!
See Also
category: Howto
The date select helper gives you an easy way to include a date selector within your application. The user selects the date from three dropdown boxes and therefore has little opportunity to make mistakes.
The date selector is created by this statement
<%= date_select( "member", "birthday", :start_year => 1900 ) %> </pre>In this example Member is a model, birthday is a attribute in the model. The selector will start in the year 1900.
The example would render to this html-code in the view :<select name="member[birthday(1i)]"> <option>1900</option> <option>1901</option> [SNIP] <option>1974</option> <option>1975</option> <option selected="selected">1976</option> <option>1977</option> <option>1978</option> <option>1979</option> <option>1980</option> <option>1981</option> </select> <select name="member[birthday(2i)]"> <option value="1">January</option> <option value="2">February</option> <option value="3" selected="selected">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> <select name="member[birthday(3i)]"> <option>1</option> <option>2</option> <option>3</option> [SNIP] <option>21</option> <option selected="selected">22</option> <option>23</option> [SNIP] <option>31</option> </select>As you can see there are the three dropdown boxes, each with a special name, so that rails can use them for a multi-value assignment.
Rails makes it simple to save the changed values.
This is an example code, taken from the member controller :
def update @member = Member.find( @params['member']['id']) if @member.update_attributes( @params['member'] ) redirect_to( :action => "list") else render_action "edit" end endMore on datehelpers is found in the reference documentation
Note on usability
I’m rather wary of this feature. If you use the database a lot, you’d want a simple text-field for date-entry and not click for every date on three select-boxes each. — vs
Defaults
It seems like it’s not possible to set a default for month, day selects. On the other hand, a default can be set for the year using :default_start_year or :start_year. — Tamer Salama?
Answer: Initialize your model with the date you want. More here .
:discard_year doesn’t work!
I have created a small plugin to adjust the behaviour to fix this issue here: http://www.markpaxton.net/RoR/datetime-fix.zip
The selectors also ignore standard rules for the number of days in the month. Each month gives 31 days as valid select options. Ugh!
See Also
category: Howto