如何修改Rails datetime_select助手的输入类型? [英] How can I modify the input type of the Rails datetime_select helper?
问题描述
我正在其中一种形式中使用Rails帮助器datetime_select
.我目前需要将day
,year
,hour
和minute
的下拉列表更改为带有验证的文本框.有没有一种方法可以指定我要这些字段的文本框?还是可能会有其他帮手来满足我的需求?
I am using the Rails helper datetime_select
in one of my forms. I currently have a requirement to change the dropdowns for day
, year
, hour
, and minute
to be textboxes with validation. Is there a way I can specify that I want textboxes for these fields? Or possibly a different helper that will do what I need?
这是我的用法
datetime_select(:campaign, :start_date, :order => [:day, :month, :year, :hour, :minute])
推荐答案
如何滚动自己的简单帮助程序,例如
how about rolling your own simple helper, like
def datetime_text_fields(object_name, method)
html = text_field_tag("#{object_name}[#{method}(3i)]", Date.today.day.to_s, :length => 2)
html << select_month(Date.today, :field_name => "#{object_name}[#{method}(2i)]")
html << text_field_tag("#{object_name}[#{method}(1i)]", Date.today.year.to_s, :length => 4)
html << " "
html << text_field_tag("#{object_name}[#{method}(4i)]", Time.now.hour.to_s, :length => 2)
html << ":"
html << text_field_tag("#{object_name}[#{method}(5i)]", Time.now.min.to_s, :length => 2)
end
可以随意添加更多格式的东西/分隔符等,但是它基本上会返回正确的字段名称,以便将Rails标识为DateTime. Rails期望使用名为date(1i)
,1i =年,2i =月等的字段.
Feel free to add more formatting stuff/separators etc. but it basically returns to correct field names for rails to be identified as DateTime. Rails expects fields named like date(1i)
, 1i = year, 2i = month etc.
老实说,我没有对其进行任何测试,但是控制台中的输出看起来很令人信服;)
Honestly I didn't test it or anything, but the output in console looked pretty convincing ;)
这篇关于如何修改Rails datetime_select助手的输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!