2013-08-18 81 views
0

我試圖驗證來自某個用戶的輸入。特別是我試圖驗證date_selecttime_select的輸入。我處理類似下面的輸入:查看的Rails時間比較

輸入標籤:

<%= date_select :date, 'date', use_short_month: true %> 
<%= time_select :Starttime , 'Starttime', default: Time.now, minute_step: 30,:ignore_date => true %> 

轉換的輸入時間對象(控制器):

@Start = Time.new(date['date(1i)'].to_i, date['date(2i)'].to_i, date['date(3i)'].to_i, t_start["Starttime(4i)"].to_i, t_start["Starttime(5i)"].to_i) 

如果我在顯示@Start查看一切正常! 2013-08-18 16:00:00 +0200

現在我試着檢查有效輸入的時間/日期。所以我在控制器中做了這個功能:

def check_date 
if @Start < Time.now 
    flash[:notice ]= "Booking has to be in the future!" 
    return false 
else 
    return true 
end 
end 

但它不起作用。 ERR_MSG。出現undefined method '<' for nil:NilClass。我不知道爲什麼。查看輸出Time.now@Start是相同的。

我也試過這與DateTime對象,而不是時間對象,但同樣的錯誤出現。有任何想法嗎?

編輯:

Index操作:

def index 
t_start= params[:Starttime] 
date= params[:date] 
if t_start && date 
    @Start = Time.new(date['date(1i)'].to_i, date['date(2i)'].to_i, date['date(3i)'].to_i, t_start["Starttime(4i)"].to_i, t_start["Starttime(5i)"].to_i) 
end 
check_date 
@tables = Table.search(params[:search]) 
end 

感謝

+0

以及您如何調用該功能? –

+0

在帶有check_date的索引操作中 – Mike

+0

粘貼您的索引方法以及 –

回答

0

我自己發現錯誤...方法調用必須是如果查詢否則@啓動內部有沒有價值。

謝謝