datetime_select 助手的问题 [英] Problem with datetime_select helper

查看:41
本文介绍了datetime_select 助手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,这是我的代码:

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...

所以我写了这段代码:

<%= 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)

推荐答案

您可以在提示中指定日期和月份:

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} %>

编辑:谢谢你抓住那个乔伊.我的解决方案不会将默认月份设置为当前月份——即使您要传入月份编号(这是数据库期望发布的月份).默认情况下会忽略提示,而且您似乎无法使用 :default:prompt 的组合.

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"}

然后使用 javascript 更改默认的月份和日期,如下所示:

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);

  }  
});  

这里假设你的模型是Comment,列是date

This assumes your model is Comment and the column is date,

这是一个工作示例.

这篇关于datetime_select 助手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆