Reputation: 181
I need some help, this is my code:
<%= f.datetime_select :date, :start_year => 2011 %>
When I write like this in my app, i get the time now, i want to get the time now in the month and the day selects and I want in the hour and minute selects not to get the hour and the minute now i want to see "hour" and "minute" string in the selects...
So i wrote this code:
<%= f.datetime_select :date, :start_year => 2011, :prompt => { :hour => "Hour", :minute
=> "minute" } %>
In the code above, the hour and the minute is good but the month and the day is not good (not the time now)
Upvotes: 1
Views: 1783
Reputation: 7303
You could specify the day and month in the prompt:
<%- t = Time.now %>
<%= f.datetime_select :date, :start_year => 2011, :prompt => { :hour => "Hour", :minute => "minute", :month => t.strftime("%B"), :day => t.day} %>
Edit:
Thanks for catching that Joey. My solution does not set the default month to the current month--even if you were to pass in the month number (which is what the database expects to be posted). Prompts are ignored by default, and it seems that you can't use a combination of :default
and :prompt
.
One alternative I can come up with is doing this:
= f.datetime_select :date, :start_year => 2011, :prompt => { :hour => "hour", :minute => "minute"}
Then using javascript to change the default month and day like this:
# somewhere in application.js
$(function () {
if ($('#new_comment').length > 0) {
var model = 'comment'
var atrib = 'date'
/* update the day */
t3 = $('#' + model + '_' + atrib + '_3i');
var dt = new Date();
t3.val(dt.getDate());
/* update the month */
t2 = $('#' + model + '_' + atrib + '_2i');
t2.val(dt.getMonth() + 1);
}
});
This assumes your model is Comment
and the column is date
,
Here's a working example.
Upvotes: 1