2014-07-26 44 views
0

enter image description here
圖片上可以看到,綠色的線組(ED)_by和排序(ED),如下面的代碼所示:
控制器:我如何排序表的內容

class WelcomeController < ApplicationController 
    def index 
    if(current_user) 
     @user_schedules = current_user.schedules 
     @user_schedules_date = @user_schedules.group_by { |tables| tables.date } 
    end 
    end 
end 


視圖:

<% @user_schedules_date.sort.each do |date, schedules| %> 
    <tr class="thead success"> 
    <th colspan="4" scope="col"><p><%= date %></p></th> 
    </tr> 
    <% for schedule in schedules %> 
    <tr> 
     <th scope="row"><p><%= schedule.titel %></p></th> 
     <td><p><%= schedule.time %></p></td> 
     <td><p><%= schedule.location %></p></td> 
    </tr> 
    <% end %> 
<% end %> 


我該如何排序「時間」?


編輯:

數據庫:

id;user_id;titel;location;time;date 
1;"12";"Test";"Testort";"12:45:00";"01.01.2014" 
2;"12";"Test2";"Testort2";"12:30:00";"01.01.2014" 
3;"12";"Test3";"Testort3";"13:00:00";"02.01.2014" 



EDIT2:

數據庫更新:

id;user_id;titel;location;time;date;date_time 
1;"12";"Test";"Testort";"12:45:00";"01.01.2014";"2014-01-01 00:00:00" 
2;"12";"Test2";"Testort2";"12:30:00";"01.01.2014";"2014-01-01 01:00:00" 
3;"12";"Test3";"Testort3";"13:00:00";"02.01.2014";"2014-02-01 00:00:00" 
4;"21";"Test4";"Testort4";"Testzeit4";"Testdatum";"2014-03-01 00:00:00" 

所以這個工作得很好。

的HTML表,現在看起來是這樣的:
enter image description here

現在我已經明白如何單獨的日期和時間以及如何排序的灰線的時間。

我不知道如何使用strftime-方法。你說我應該在模型中實現它?
我在排程模型試過這樣:

def get_date 
    return date_time.strftime("%d.%m.%Y"); 
end 


我應該添加視圖代碼

<% @user_schedules_date.sort.each do |date_time, schedules| %> 
    <tr class="thead success"> 
    <th colspan="4" scope="col"><p><%= date_time %></p></th> 
    </tr> 
    <% for schedule in schedules %> 
    <tr> 
     <th scope="row"><p><%= schedule.titel %></p></th> 
     <td><p><%= schedule.time %></p></td> 
     <td><p><%= schedule.location %></p></td> 
    </tr> 
    <% end %> 
<% end %> 

這將是很好,如果你能再幫我。
謝謝。

回答

2

您的time字段當前是字符串。這意味着,只需撥打sorttime字段將做你想做的。相反,它將按照字母數字順序進行排序。例如1:00 PM將會在10:00 AM之前,因爲:小於0

爲了解決這個問題,你可以做兩件事情之一:

解析的時候,你需要做的排序,即,像這樣:

@user_schedules_date = 
    @user_schedules.group_by(&:date) 
    .each do |(_, grp)| 
     grp.sort_by! { |row| Time.parse(row.time) } 
    end 

或者(這是什麼我會推薦)。將datetime合併爲一個字段(我們稱之爲date_time),其類型爲datetime。通過這種方式,Rails知道在給它之前將它從數據庫中獲得的值包含在DateTime的實例中,並知道將該值作爲DATETIME字段存儲在數據庫中。

這是偉大的,因爲那時它簡化了您的分組和排序,只是這樣的:

@user_schedules_date = 
    @user_schedules.order(:date_time) 
    .group_by { |sched| sched.date_time.beginning_of_day } 

而且更重要的是,排序(排序)是由數據庫完成的,而不是經過我們裝成Ruby的內存,這在大多數情況下是可取的。

EDIT(要回答的問題編輯)

  • 來獲得日期字符串從DateTime實例回來,你就以下date.strftime("%d.%m.%Y")
  • 同樣,對於日程安排,獲取時間字符串完成如下:schedule.date_time.strftime('%T')
+0

Date_Time機會看起來不錯。我會盡快進行測試。我可以分離日期時間,如下所示:datetime.time和datetime.date?我從Java和C#知道這一點,我想在用戶輸入和表格視圖中將它分開。謝謝你的好解釋。 – Ismoh

+0

如果你想單獨的日期和時間字符串,那麼是的,這是可能的,使用['DateTime#strftime'](http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime。 html#method-i-strftime)(您應該將這些定義爲模型演示者的方法)。 – amnn

+0

你能解釋一下嗎?我不明白它atm:'未定義的方法'beginning_of_day'爲零:NilClass' – Ismoh