2011-11-07 40 views
1

如何處理這兩個輸入,以便兩者都可以發送到控制器並保存爲日期時間?或者是任何幫助者/寶石看起來像來自html5的日期和時間選擇器?如何將html5輸入類型日期/時間的數據保存爲Rails中的日期時間?

HTML5輸入:

%input{:type => "date", :value => "yyyy-mm-dd"} 
%input{:type => "time", :value => "07:00"} 

標準幫手:

= f.datetime_select :time 

編輯: 發生,我簡單的解決方案我檢查,並將其與時間字段工程:

%input{:name => "model[start_at]", :type => "date", :value => "yyyy-mm-dd"} 
%input{:name => "model[start_at]", :type => "time", :value => "07:00"} 

回答

2

假設你的日期時間場在模型中

1),定義這兩個方法:d被稱爲 「started_at」,試試這個在您看來

def started_at_string 
    # return started_at datetime converted to a string in a desired format, e.g.: 
    self.started_at.strftime(Time::DATE_FORMATS[:default]) 
end 

started_at_string=(datetime_string) 
    self.started_at = Time.zone.parse(datetime_string) 
    # also you should process cases when the string is not a datetime 
end 

2):

f.text_field :started_at_string 

3)應用此日期選擇器到文本字段: http://jqueryui.com/demos/datepicker/,這次選擇器在頂部:http://trentrichardson.com/examples/timepicker/,你會得到一個不錯的日期時間選擇器。

儘管(屏幕鍵盤和選擇器同時彈出並且難以輸入日期),但此方法對於iPhone來說工作不太好。

相關問題