2011-06-16 48 views
0

我有一個「Log」項目,每個日誌都有:日期,:小時,:描述。我只是試圖確定一週內工作了多少小時,但是無法確定代碼的正確分離。讓我知道是否需要任何進一步的代碼。軌道3Ruby on Rails:本週工作的小時數

log.rb

def self.days_in_range(from, to) 
    Log.where(:date => (from.to_date)..(to.to_date)) 
end 

index.html.erb

<% content_for :sidebar do %> 
<h4> Sidebar Content </h4> 
<ul> 
    <li>Hours worked this week: 
    <%= Log.hours_this_week %> # unsure how to call 
    </li> 
    <li>Hours worked in total: 
    <%= Log.sum(:hours) %> 
    </li> 
    <li>Most hours worked in a day: 
    <%= Log.maximum(:hours) %> 
    </li> 
</ul> 
<% end %> 

logs_helper.rb?

def hours_this_week 
    today = Time.now 
    day_of_week = today.wday 
    sunday = today - day_of_week.days 
    days = Log.days_in_range(today, sunday) 
    hours = 0 

    days.each do |day| 
    hours += day.hours 
    end 

end 

[解決] 錯誤

Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised: 

undefined method `hours_this_week' for #<LogsController:0x103b66be8> 
Extracted source (around line #33): 

30:  <h4> Sidebar Content </h4> 
31:  <ul> 
32:   <li>Hours worked this week: 
33:    <%= hours_this_week %> 
34:   </li> 
35:   <li>Hours worked in total: 
36:    <%= Log.sum(:hours) %> 
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours 

full trace

[更新] 新的錯誤

錯誤

ArgumentError in Logs#index 

Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised: 

wrong number of arguments (0 for 1) 
Extracted source (around line #33): 

30:  <h4> Sidebar Content </h4> 
31:  <ul> 
32:   <li>Hours worked this week: 
33:    <%= hours_this_week %> 
34:   </li> 
35:   <li>Hours worked in total: 
36:    <%= Log.sum(:hours) %> 
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours 
+0

LogsHelper模塊內部的幫助器方法? – 2011-06-16 17:17:41

+0

是的,但是我從我的觀點來看,這是否有效? – Adam 2011-06-16 17:29:01

回答

0

這是切你的問題,但我注意到這個看你hours_this_week方法。我可能是錯的,但您可能想要查看的一件小事是您的hours_this_week方法將返回您的each語句(即days)中迭代的集合,而不是該語句的產品(新值hours )。

你既可以只添加一行:

hours 

這種方法的結束,或者使用的inject代替each

# The initial "hours" declaration is no longer necessary, 
# because inject returns its result, rather than the 
# collection it is iterating through. 

days.inject(0) {|hours, day| hours += day.hours } 

這條線將消除您的each聲明的必要性並在hours_this_week方法結束時明確返回hours

這就是說,把方法logs_helper.rb,並調用它:

<%= hours_this_week %> 

將要走的路。

+0

'days.sum(&:hours)'更好:-) – 2011-06-16 17:16:34

+0

啊,你說的沒錯,我在看Ruby,忘記了ActiveRecord的方法 – clem 2011-06-16 17:20:26

+0

我都沒有:-)這是一個數組實例方法,我認爲它在stdlib中,但實際上它是由Active Support提供的。 – 2011-06-16 17:45:44

0

假設你index.html.erb文件在app/views/logs文件夾,你可以叫hours_this_week直接:

<%= hours_this_week %> 
+0

當我這樣做時,出現以下錯誤:未定義的方法'hours_this_week'爲#。另外,是logs_helper.rb合適的文件來寫我的方法? – Adam 2011-06-16 17:03:37

+0

是'app/helpers'中的'logs_helper.rb'文件嗎?你是否試圖從你的實際控制器或視圖內調用'hours_this_week'? – 2011-06-16 17:15:01

+0

您應該正確地將其稱爲@dmarkow如何寫入它,而不是使用'<%= Log.hours_this_week%>'。 – clem 2011-06-16 17:15:28